-
Notifications
You must be signed in to change notification settings - Fork 333
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 #3009
base: 6
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
use SilverStripe\Forms\FieldList; | ||
use SilverStripe\Forms\HeaderField; | ||
use SilverStripe\Forms\OptionsetField; | ||
use SilverStripe\Forms\UrlField; | ||
use SilverStripe\Versioned\Versioned; | ||
|
||
/** | ||
|
@@ -47,6 +48,9 @@ class RedirectorPage extends Page | |
'RedirectionType', | ||
'Content', | ||
], | ||
'fieldClasses' => [ | ||
'ExternalURL' => UrlField::class, | ||
], | ||
]; | ||
|
||
private static $table_name = 'RedirectorPage'; | ||
|
@@ -171,35 +175,12 @@ public function syncLinkTracking() | |
} | ||
} | ||
|
||
protected function onBeforeWrite() | ||
{ | ||
parent::onBeforeWrite(); | ||
|
||
if ($this->ExternalURL && substr($this->ExternalURL ?? '', 0, 2) !== '//') { | ||
$urlParts = parse_url($this->ExternalURL ?? ''); | ||
if ($urlParts) { | ||
if (empty($urlParts['scheme'])) { | ||
// no scheme, assume http | ||
$this->ExternalURL = 'http://' . $this->ExternalURL; | ||
} elseif (!in_array($urlParts['scheme'], [ | ||
'http', | ||
'https', | ||
])) { | ||
// we only allow http(s) urls | ||
$this->ExternalURL = ''; | ||
} | ||
} else { | ||
// malformed URL to reject | ||
$this->ExternalURL = ''; | ||
} | ||
} | ||
} | ||
Comment on lines
-174
to
-196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to set false if it's invalid, because the |
||
|
||
public function getCMSFields() | ||
{ | ||
$this->beforeUpdateCMSFields(function (FieldList $fields) { | ||
// Remove all metadata fields, does not apply for redirector pages | ||
$fields->removeByName('Metadata'); | ||
$fields->dataFieldByName('ExternalURL')?->setAllowRelativeProtocol(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per the first condition in the now-removed |
||
|
||
$fields->addFieldsToTab( | ||
'Root.Main', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to keep this is as you can programatically save things without it going via a UrlField
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That felt very much like it was being done instead of validation. I feel like validating the value in the better option.
I can add the URL validation in the
validate()
method for this class if you like? But I don't think it makes sense to alter the value that's being set, especially programatic values.