Adding Custom Validation Rules in Valitron

Valitron is a simple, minimal and elegant stand-alone validation library with NO dependencies. Valitron uses simple, straightforward validation methods with a focus on readable and concise syntax. Valitron is the simple and pragmatic validation library we’ve been looking for.

Adding Custom Validation Rules :

To add your own validation rule, use the addInstanceRule method with a rule name, a custom callback or closure, and a error message to display in case of an error. The callback provided should return boolean true or false.

such as :

$validation->addInstanceRule(‘functionName’, function () {
return false;
});

$validation->rule(‘functionName’, ‘fieldName’)->message(‘Error Message’);

 

We can also use the following structure :

if (condition) {

$validation->addInstanceRule(‘functionName’, function () {
return false;
});

$validation->rule(‘functionName’, ‘fieldName’)->message(‘Error Message’);

}