The plugin comes with some inbuilt validation rules. Here is the list of the inbuilt rules:
| Name | Description |
|---|---|
| to validate email addresses. | |
| Equality | to check the equality of 2 values. |
| Exclusion | to check the value exclusion from the provided list. |
| FileMaxCount | to check the file count against the provided maximum. |
| FileMaxSize | to check file size against the provided maximum. |
| FileMimeType | to check file mime type presence in the provided list. |
| Format | to check the value format against the provided regular expression. |
| Required | to check non-empty value. |
| URL | to 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:
public static function getName: This method is designed to return the rule’s unique name. The return value should be a string.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.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.protected function validate: This method is designed to implement the validation. It receives 2 arguments:
| Name | Type | Description |
|---|---|---|
$value | mixed | The value to validate. |
$submission | \XPACGroup\Plugin\XPACForms\Core\Submission | The 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.
| Name | Type | Description |
|---|---|---|
$block | array | An associative array representing the parsed block. |
$options | array | Additional 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.
| Name | Type | Description |
|---|---|---|
$message | string | The actual message of the rule. |
$rule | \XPACGroup\Plugin\XPACForms\Rules\Rule | null | Optional: 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
);