Validation
Validation is the process of checking if the data entered by the user is valid and meets certain criteria. It is an important step in ensuring that the data entered by the user is accurate and complete.
Metapress provides a simple way to validate data using the Validator class. The validation functionality included in the framework comes with a set of built-in validation rules that can be used to validate different types of data.
Validating data
To validate data, you must create a new instance of the Validator class and then use the validate() method to validate the data.
use Sematico\Metapress\Validator\Validator;
$validator = new Validator();Now that you have a Validator instance, you can use the validate() method to validate the data.
The validate() method takes an array of validation rules as first argument and the data to be validated as the second argument.
$data = [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
];
$rules = [
'name' => 'required|string|min:10|max:20',
'email' => 'email',
];
$validator->validate( $rules, $data );In this example, we have an array of validation rules and an array of data to be validated. The validate() method will check if the data meets the specified validation rules and store the validation results in an array.
Validation results
After validating the data, you can access the validation results using the get_errors() method of the Validator class.
$errors = $validator->get_errors();This will return an array of validation errors, for each field that failed validation.
Available validation rules
The Validator class comes with a set of built-in validation rules that can be used to validate different types of data. Here are some examples of the available validation rules:
email: Validates that the data is a valid email address.required: Validates that the data is not empty.string: Validates that the data is a string.empty: Validates that the data is empty.min:value: Validates that the data is greater than or equal to a specified value.max:value: Validates that the data is less than or equal to a specified value.numeric: Validates that the data is a numeric value.
Custom validation rules
If you need to create your own custom validation rules, you can do so by extending the AbstractRule class and implement the check method, the get_message method and the get_rule_name method.
use Sematico\Metapress\Validator\Rules\AbstractRule;
class MyCustomRule extends AbstractRule
{
private $ruleName = 'my_custom_rule';
public function check($value)
{
// Your custom validation logic here
}
public function get_message()
{
// Your custom error message here
}
public function get_rule_name()
{
// Your custom rule name here
}
}Each custom rule should have a unique name and a message that describes the error that will be returned when the rule fails. Remember to set the $ruleName property as this will be used to specify the rule name when validating data.
Now that you have created your custom rule, you can use the add_rule() method of the Validator class to add it to the validation rules.
$validator->add_rule( new MyCustomRule() );