Skip to content

Commit

Permalink
ENH Use FieldValidator for FormFields
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Oct 31, 2024
1 parent 3939401 commit d11a116
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 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,21 @@ 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);
$result = ValidationResult::create();
$this->beforeExtending('updateValidate', function() use ($result) {
if ($this->checkHostname($this->Value())) {
return;
}
$result->addFieldError(
$this->getName(),
_t('DomainNameField.INVALID_DOMAIN', 'Invalid domain name'),
'validation'
);
});
return $result->combineAnd(parent::validate());
}

/**
Expand Down

0 comments on commit d11a116

Please sign in to comment.