Topics

Data validators

ImpressPages 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()
Email 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 Ip\Form\Validator class. Custom validator should extend getError() method and optional __construct() method.

<?php
namespace Plugin\MySamplePlugin;

class MyCustomValidator extends \Ip\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;
        }
    }

}

See also

comments powered by Disqus