Documentation > Developers > Backend validation rules

Backend validation rules

The plugin comes with some inbuilt validation rules. Here is the list of the inbuilt rules:

NameDescription
Emailto validate email addresses.
Equalityto check the equality of 2 values.
Exclusionto check the value exclusion from the provided list.
FileMaxCountto check the file count against the provided maximum.
FileMaxSizeto check file size against the provided maximum.
FileMimeTypeto check file mime type presence in the provided list.
Formatto check the value format against the provided regular expression.
Requiredto check non-empty value.
URLto validate URLs.

Besides the built-in validation rules, the plugin also provides a possibility to register custom validation rules. Creating a custom validation rule is as easy as creating a child class of \XPACGroup\Plugin\XPACForms\Rules\Rule class. The parent class implements all required logic and requires 4 abstract methods to be implemented in the child class:

  1. public static function getName: This method is designed to return the rule’s unique name. The return value should be a string.
  2. protected function isActive: This method is designed to indicate whether the validation rule should validate the value. It receives an associative array representing the parsed block. The return value should be a boolean true – indicating that the rule should validate the value; a boolean false – vice versa.
  3. public static function getMessage: This method is designed to return the validation message of the rule. It optionally receives its own instance during the validation. The return value should be a string representing the validation rule for the user.
  4. protected function validate: This method is designed to implement the validation. It receives 2 arguments:
NameTypeDescription
$valuemixedThe value to validate.
$submission\XPACGroup\Plugin\XPACForms\Core\SubmissionThe form submission instance.

It should implement the validation logic and return a boolean true indicating that the value is valid, and a boolean false – otherwise.

Optionally, the constructor method of the rule class is available to be overwritten in the child class, which will receive 2 arguments.

NameTypeDescription
$blockarrayAn associative array representing the parsed block.
$optionsarrayAdditional options passed to the rule during block type registration to the form.

Don’t forget to call the parent::__construct($block, $options); in the rule constructor during the overwriting.

<?php

class CustomRule extends Rule
{
    /**
     * Get a unique name.
     * @return string
     */
    final public static function getName(): string
    {
        return 'custom-rule';
    }

    /**
     * Check whether the rule is active.
     * @param   array  $block  Parsed block.
     * @return bool
     */
    protected function isActive(array $block): bool
    {
        if ('some_field_name' === $block['attrs']['name']) {
            return true;
        }
        return false;
    }

    /**
     * Get the error message.
     * @param   XPACGroup\Plugin\XPACForms\Rules\Rule|null  $rule  Optional: Rule instance.
     * @return string
     */
    public static function getMessage($rule = null)
    {
        $message = __('My awesome validation message', 'textdomain');
        return parent::prepareMessage($message, $rule);
    }

    /**
     * Validate provided value.
     * @param   mixed                                        $value       Value to validate.
     * @param   \XPACGroup\Plugin\XPACForms\Core\Submission  $submission     Submission instance.
     *
     * @return bool
     */
    public function validate($value, $submission): bool
    {
        return 10 === $value;
    }
}

The parent rule-class additionally implements the validation message modification functionality via a dynamic xpac_forms_validation_{$name}_rule_message filter, where the $name is the rule’s unique name and provides a possibility to overwrite default validation messages. The callback hooked in the mentioned dynamic filter will receive the 2 arguments and should return the updated validation message.

NameTypeDescription
$messagestringThe actual message of the rule.
$rule\XPACGroup\Plugin\XPACForms\Rules\Rule | nullOptional: the rule instance.

Example:

function edit_required_rule_message($message, $rule) {
	return "Hello World";
}
add_filter( 
     'xpac_forms_validation_required_rule_message', 
     'edit_required_rule_message', 
     10,
     2 
);