Skip to content

Commit

Permalink
API Use updated FormField::validate() signature
Browse files Browse the repository at this point in the history
emteknetnz committed Nov 27, 2024
1 parent db69d15 commit 10bbdda
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/Forms/WildcardDomainField.php
Original file line number Diff line number Diff line change
@@ -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
@@ -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();
}

/**
@@ -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()

0 comments on commit 10bbdda

Please sign in to comment.