Custom Validation Rules
In certain scenarios, you might need to implement form validation checks beyond those provided by Trongate's Validation class. This is where custom form validation callbacks are useful.
General Concept
When using Trongate's Validation class to apply form validation checks, a critical part of the process involves using set_rules to specify the validation rules for particular form fields. For example, the following code enforces that both 'first_name' and 'last_name' fields must:
- Not be empty
- Be at least 2 characters long
- Be no more than 255 characters long
$this->validation_helper->set_rules('first_name', 'first name', 'required|min_length[2]|max_length[255]');
$this->validation_helper->set_rules('last_name', 'last name', 'required|min_length[2]|max_length[255]');
Adding Custom Validation Tests
To enforce unique validation rules not covered by Trongate’s built-in tests, you can create custom validation callbacks. For instance, if you want to enforce the rule that: "The last name cannot be 'Rambo'", follow the steps below.
- Declare the custom validation test.
- Create a method that defines the behavior of the custom validation test.
Declaring a Custom Validation Test
To declare a custom validation rule, prepend the rule with 'callback_
' followed by the name of the method that will handle the validation test. For example, if you have a method named 'check_last_name
' for custom validations, append the following to the set_rules declaration for 'last_name':
|callback_check_last_name
This change means your block of code for applying form validation tests would now be as follows:
$this->validation_helper->set_rules('first_name', 'first name', 'required|min_length[2]|max_length[255]');
$this->validation_helper->set_rules('last_name', 'last name', 'required|min_length[2]|max_length[255]|callback_check_last_name');
Creating a Custom Validation Method
Custom validation methods will automatically receive the posted form field value as an argument and should return either a boolean of true
(indicating validation success) or an error message (as a string). The following code demonstrates the basic structure for a typical custom validation callback method:
function check_last_name($last_name) {
// Perform some tests
}
To define the behavior of your custom validation rule, you can use the following implementation:
function check_last_name($last_name) {
if ($last_name === 'Rambo') {
return 'You know, we don\'t want guys like you in this town.';
} else {
return true;
}
}
If the submitted last_name
value fails our custom validation test, the following validation error will be returned by the method:
You know, we don't want guys like you in this town.
This validation error message can then - optionally - be rendered onto the page, giving the user an opportunity to correct the input.
However, if the submitted last_name
passes our custom validation test, the method will return a boolean of true
, and no validation errors will be produced by the method.
For improved code clarity and functionality, consider applying doc blocks, access modifiers, type hints, and return types to your validation callback methods. Although optional, these practices enhance code readability and maintainability. For example:
/**
* Validates the last name to ensure it is not 'Rambo'.
*
* @param string $last_name The last name to validate.
* @return string|true Returns an error message if the last name is 'Rambo', otherwise returns true.
*/
function check_last_name(string $last_name): string|true {
if ($last_name === 'Rambo') {
return 'You know, we don\'t want guys like you in this town.';
} else {
return true;
}
}
The next section will explore how server-side form validation tests are handled with Trongate.