Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Use symfony/validation logic #99

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/IFramePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace SilverStripe\IFrame;

use Page;
use SilverStripe\Core\Validation\ConstraintValidator;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\Core\Validation\ValidationException;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used

use SilverStripe\Core\Validation\ValidationResult;
use Symfony\Component\Validator\Constraints\Url;

/**
* Iframe page type embeds an iframe of URL of choice into the page.
Expand Down Expand Up @@ -119,21 +120,21 @@ public function getStyle()
/**
* Ensure that the IFrameURL is a valid url and prevents XSS
*
* @throws ValidationException
* @return ValidationResult
*/
public function validate()
{
$result = parent::validate();

//whitelist allowed URL schemes
$allowed_schemes = array('http', 'https');
if ($matches = parse_url($this->IFrameURL ?? '')) {
if (isset($matches['scheme']) && !in_array($matches['scheme'], $allowed_schemes ?? [])) {
$result->addError(_t(__CLASS__ . '.VALIDATION_BANNEDURLSCHEME', "This URL scheme is not allowed."));
}
}

return $result;
$fullResult = parent::validate();

$allowedSchemes = ['http', 'https'];
$message = _t(__CLASS__ . '.VALIDATION_URL', 'Please enter a valid URL');
$result = ConstraintValidator::validate(
$this->value,
new Url(message: $message, protocols: $allowedSchemes),
$this->getName()
);
$fullResult->combineAnd($result);

return $fullResult;
}
}
Loading