Skip to content

Commit

Permalink
NEW: Add extension hook for field-specific validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kinglozzer committed Feb 20, 2023
1 parent 6a6a784 commit 97f7be5
Show file tree
Hide file tree
Showing 19 changed files with 248 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/Forms/CompositeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,6 @@ public function validate($validator)
/** @var FormField $child */
$valid = ($child && $child->validate($validator) && $valid);
}
return $valid;
return $this->extendValidationResult($valid, $validator);
}
}
18 changes: 9 additions & 9 deletions src/Forms/ConfirmedPasswordField.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function validate($validator)

// if field isn't visible, don't validate
if (!$this->isSaveable()) {
return true;
return $this->extendValidationResult(true, $validator);
}

$this->getPasswordField()->setValue($this->value);
Expand All @@ -431,7 +431,7 @@ public function validate($validator)
"validation"
);

return false;
return $this->extendValidationResult(false, $validator);
}

if (!$this->canBeEmpty) {
Expand All @@ -443,7 +443,7 @@ public function validate($validator)
"validation"
);

return false;
return $this->extendValidationResult(false, $validator);
}
}

Expand Down Expand Up @@ -483,7 +483,7 @@ public function validate($validator)
"validation"
);

return false;
return $this->extendValidationResult(false, $validator);
}
}

Expand All @@ -498,7 +498,7 @@ public function validate($validator)
"validation"
);

return false;
return $this->extendValidationResult(false, $validator);
}
}

Expand All @@ -513,7 +513,7 @@ public function validate($validator)
),
"validation"
);
return false;
return $this->extendValidationResult(false, $validator);
}

// Check this password is valid for the current user
Expand All @@ -527,7 +527,7 @@ public function validate($validator)
),
"validation"
);
return false;
return $this->extendValidationResult(false, $validator);
}

// With a valid user and password, check the password is correct
Expand All @@ -543,12 +543,12 @@ public function validate($validator)
),
"validation"
);
return false;
return $this->extendValidationResult(false, $validator);
}
}
}

return true;
return $this->extendValidationResult(true, $validator);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Forms/CurrencyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function performReadonlyTransformation()

public function validate($validator)
{
$result = true;
$currencySymbol = preg_quote(DBCurrency::config()->uninherited('currency_symbol') ?? '');
$regex = '/^\s*(\-?' . $currencySymbol . '?|' . $currencySymbol . '\-?)?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\s*$/';
if (!empty($this->value) && !preg_match($regex ?? '', $this->value ?? '')) {
Expand All @@ -66,9 +67,10 @@ public function validate($validator)
_t('SilverStripe\\Forms\\Form.VALIDCURRENCY', "Please enter a valid currency"),
"validation"
);
return false;
$result = false;
}
return true;

return $this->extendValidationResult($result, $validator);
}

public function getSchemaValidation()
Expand Down
10 changes: 5 additions & 5 deletions src/Forms/DateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function validate($validator)
{
// Don't validate empty fields
if (empty($this->rawValue)) {
return true;
return $this->extendValidationResult(true, $validator);
}

// We submitted a value, but it couldn't be parsed
Expand All @@ -382,7 +382,7 @@ public function validate($validator)
['format' => $this->getDateFormat()]
)
);
return false;
return $this->extendValidationResult(false, $validator);
}

// Check min date
Expand All @@ -406,7 +406,7 @@ public function validate($validator)
ValidationResult::TYPE_ERROR,
ValidationResult::CAST_HTML
);
return false;
return $this->extendValidationResult(false, $validator);
}
}

Expand All @@ -431,11 +431,11 @@ public function validate($validator)
ValidationResult::TYPE_ERROR,
ValidationResult::CAST_HTML
);
return false;
return $this->extendValidationResult(false, $validator);
}
}

return true;
return $this->extendValidationResult(true, $validator);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Forms/DatetimeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ public function validate($validator)
{
// Don't validate empty fields
if (empty($this->rawValue)) {
return true;
return $this->extendValidationResult(true, $validator);
}

// We submitted a value, but it couldn't be parsed
Expand All @@ -578,7 +578,7 @@ public function validate($validator)
['format' => $this->getDatetimeFormat()]
)
);
return false;
return $this->extendValidationResult(false, $validator);
}

// Check min date (in server timezone)
Expand All @@ -602,7 +602,7 @@ public function validate($validator)
ValidationResult::TYPE_ERROR,
ValidationResult::CAST_HTML
);
return false;
return $this->extendValidationResult(false, $validator);
}
}

Expand All @@ -627,11 +627,11 @@ public function validate($validator)
ValidationResult::TYPE_ERROR,
ValidationResult::CAST_HTML
);
return false;
return $this->extendValidationResult(false, $validator);
}
}

return true;
return $this->extendValidationResult(true, $validator);
}

public function performReadonlyTransformation()
Expand Down
5 changes: 3 additions & 2 deletions src/Forms/EmailField.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function Type()
*/
public function validate($validator)
{
$result = true;
$this->value = trim($this->value ?? '');

$pattern = '^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$';
Expand All @@ -43,10 +44,10 @@ public function validate($validator)
'validation'
);

return false;
$result = false;
}

return true;
return $this->extendValidationResult($result, $validator);
}

public function getSchemaValidation()
Expand Down
7 changes: 4 additions & 3 deletions src/Forms/FileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function validate($validator)
$fieldName = preg_replace('#\[(.*?)\]$#', '', $this->name ?? '');

if (!isset($_FILES[$fieldName])) {
return true;
return $this->extendValidationResult(true, $validator);
}

if ($isMultiFileUpload) {
Expand All @@ -204,11 +204,12 @@ public function validate($validator)
$isValid = false;
}
}
return $isValid;
return $this->extendValidationResult($isValid, $validator);
}

// regular single-file upload
return $this->validateFileData($validator, $_FILES[$this->name]);
$result = $this->validateFileData($validator, $_FILES[$this->name]);
return $this->extendValidationResult($result, $validator);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions src/Forms/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1219,18 +1219,29 @@ public function Type()
return strtolower(preg_replace('/Field$/', '', $type->getShortName() ?? '') ?? '');
}

/**
* Utility method to call an extension hook which allows the result of validate() calls to be adjusted
*
* @param bool $result
* @param Validator $validator
* @return bool
*/
protected function extendValidationResult(bool $result, Validator $validator): bool
{
$this->extend('updateValidationResult', $result, $validator);
return $result;
}

/**
* Abstract method each {@link FormField} subclass must implement, determines whether the field
* is valid or not based on the value.
*
* @todo Make this abstract.
*
* @param Validator $validator
* @return bool
*/
public function validate($validator)
{
return true;
return $this->extendValidationResult(true, $validator);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/LookupField.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function Field($properties = [])
*/
public function validate($validator)
{
return true;
return $this->extendValidationResult(true, $validator);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Forms/MoneyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,12 @@ public function validate($validator)
['currency' => $currency]
)
);
return false;
return $this->extendValidationResult(false, $validator);
}

// Field-specific validation
return $this->fieldAmount->validate($validator) && $this->fieldCurrency->validate($validator);
$result = $this->fieldAmount->validate($validator) && $this->fieldCurrency->validate($validator);
return $this->extendValidationResult($result, $validator);
}

public function setForm($form)
Expand Down
28 changes: 15 additions & 13 deletions src/Forms/MultiSelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,21 +247,23 @@ function ($userValue) use ($self, $validValues) {
return true;
}
);
if (empty($invalidValues)) {
return true;

$result = true;
if (!empty($invalidValues)) {
$result = false;
// List invalid items
$validator->validationError(
$this->getName(),
_t(
'SilverStripe\\Forms\\MultiSelectField.SOURCE_VALIDATION',
"Please select values within the list provided. Invalid option(s) {value} given",
['value' => implode(',', $invalidValues)]
),
"validation"
);
}

// List invalid items
$validator->validationError(
$this->getName(),
_t(
'SilverStripe\\Forms\\MultiSelectField.SOURCE_VALIDATION',
"Please select values within the list provided. Invalid option(s) {value} given",
['value' => implode(',', $invalidValues)]
),
"validation"
);
return false;
return $this->extendValidationResult($result, $validator);
}

/**
Expand Down
23 changes: 12 additions & 11 deletions src/Forms/NumericField.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,21 @@ public function getAttributes()
*/
public function validate($validator)
{
$result = true;
// false signifies invalid value due to failed parse()
if ($this->value !== false) {
return true;
if ($this->value === false) {
$validator->validationError(
$this->name,
_t(
'SilverStripe\\Forms\\NumericField.VALIDATION',
"'{value}' is not a number, only numbers can be accepted for this field",
['value' => $this->originalValue]
)
);
$result = false;
}

$validator->validationError(
$this->name,
_t(
'SilverStripe\\Forms\\NumericField.VALIDATION',
"'{value}' is not a number, only numbers can be accepted for this field",
['value' => $this->originalValue]
)
);
return false;
return $this->extendValidationResult($result, $validator);
}

public function getSchemaValidation()
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/OptionsetField.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function Field($properties = [])
public function validate($validator)
{
if (!$this->Value()) {
return true;
return $this->extendValidationResult(true, $validator);
}

return parent::validate($validator);
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/SingleLookupField.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function valueToLabel()
*/
public function validate($validator)
{
return true;
return $this->extendValidationResult(true, $validator);
}

/**
Expand Down
Loading

0 comments on commit 97f7be5

Please sign in to comment.