Data validators
Construct comes with the following default validators:
| Validator name | Parameters | Description |
| Antispam | No | Prevents form spamming. Generates hidden fields for spam check. Included in form by default. Can be remove dy $form->removeSpamCheck() |
| Csrf | No | Prevents crossite request forgery. Included in form by default. Can be removed by $form->removeCsrfCheck() |
| No | Checks for valid e-mail address | |
| InArray |
Array of strings |
Checks if a value is in specified array |
| NotInArray | Array of strings | Checks if a value is not in specified array |
| Number | No | Check if a value is a number |
| Regex | Regular expression | Validates a value against regular expression |
| Required | No | Checks if a value is not blank |
Creating a custom validator
To create your custom data validator you need to extend Construct\Form\Validator class. Custom validator should extend getError() method and optional __construct() method.
<?php
namespace Plugin\MySamplePlugin;
class MyCustomValidator extends \Construct\Form\Validator {
// public function __construct($data = array(), $errorMessage = null) {
// }
public function getError($values, $valueKey, $environment) {
if (empty($values[$valueKey])) {
return false;
}
$value = $values[$valueKey];
if (!preg_match('/^[a-zA-Z0-9]*$/', $value)) {
$errorText = __('ID should contain alphanumeric characters only.', 'MySamplePlugin');
return $errorText;
} else {
return false;
}
}
}