Skip to content

Commit

Permalink
API Use updated FormField::validate() signature
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 13, 2024
1 parent 3939401 commit 39968c6
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 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,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 All @@ -36,7 +36,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

0 comments on commit 39968c6

Please sign in to comment.