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

API Use updated FormField::validate() signature #608

Merged
Merged
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
29 changes: 14 additions & 15 deletions src/Forms/WildcardDomainField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace SilverStripe\Subsites\Forms;

use SilverStripe\Forms\TextField;
use SilverStripe\Core\Validation\ValidationResult;

/**
* A text field that accepts only valid domain names, but allows the wildcard (*) character
Expand All @@ -10,22 +11,20 @@ class WildcardDomainField extends TextField
{
/**
* Validate this field as a valid hostname
*
* @param Validator $validator
* @return bool
*/
public function validate($validator)
public function validate(): ValidationResult
{
if ($this->checkHostname($this->Value())) {
return $this->extendValidationResult(true, $validator);
}

$validator->validationError(
$this->getName(),
_t('DomainNameField.INVALID_DOMAIN', 'Invalid domain name'),
'validation'
);
return $this->extendValidationResult(false, $validator);
$this->beforeExtending('updateValidate', function (ValidationResult $result) {
if ($this->checkHostname($this->Value())) {
return;
}
$result->addFieldError(
$this->getName(),
_t('DomainNameField.INVALID_DOMAIN', 'Invalid domain name'),
'validation'
);
});
return parent::validate();
}

/**
Expand All @@ -36,7 +35,7 @@ public function validate($validator)
*/
public function checkHostname($hostname)
{
return (bool)preg_match('/^([a-z0-9\*]+[\-\.\:])*([a-z0-9\*]+)$/', $hostname ?? '');
return (bool) preg_match('/^([a-z0-9\*]+[\-\.\:])*([a-z0-9\*]+)$/', $hostname ?? '');
}

public function Type()
Expand Down
Loading