Validation
Validation is a process of checking to ensure that the data entered is sensible and reasonable and that it fulfills its intended purpose.
Quantum provides a `Validation`
library which built on Wixel/GUMP validation class and makes the validation process easy and transparent.
To start validating the data you will need to specify validation rules. In the Quantum framework validation rules can be placed on controllers
or on modules
.
A simple rules configuration has similar structure:
$rules = [
'first_name' => 'required',
'last_name' => 'required',
'age' => 'numeric',
'email' => 'required|valid_email|unique,User',
'phone' => 'numeric'
];
Once rules are spcified, we can check the input parameters against validation rules
$validated = Validation::is_valid(Request::all(), $rules);
The is_valid()
method of Validation
class returns true
if data is valid, otherwhise error message will be returned.
if ($validated === true) {
// Data is valid
}
else {
// Data is invalid and $validated contains the error messages
}
Below is a list of validation rules that are available to use:
required
- Ensures the specified key value exists and is not emptyvalid_email
- Checks for a valid email addressmax_len,n
- Checks key value length, makes sure it's not longer than the specified (n) length.min_len,n
- Checks key value length, makes sure it's not shorter than the specified (n) length.exact_len
- Ensures that the key value length precisely matches the specified (n) length.alpha
- Ensure only alpha characters are present in the key valuealpha_numeric
- Ensure only alpha-numeric characters are present in the key valuealpha_dash
- Ensure only alpha-numeric characters + dashes and underscores are present in the key valuenumeric
- Ensure only numeric key valuesinteger
- Ensure only integer key valuesboolean
- Checks for PHP accepted boolean values, returns TRUE for "1", "true", "on" and "yes"float
- Checks for float valuesvalid_url
- Check for valid URL or subdomainurl_exists
- Check to see if the url exists and is accessiblevalid_ip
- Check for valid generic IP addressvalid_cc
- Check for a valid credit card number (Uses the MOD10 Checksum Algorithm)valid_name
- Check for a valid credit card number (Uses the MOD10 Checksum Algorithm)contains,n
- Verify that a value is contained within the pre-defined value setcontains_list
- validation.contains_liststreet_address
- validation.street_addressdate
- Determine if the provided input is a valid date (ISO 8601)min_numeric
- Determine if the provided numeric value is higher or equal to a specific valuemax_numeric
- Determine if the provided numeric value is lower or equal to a specific valuephone_number
- Validate phone numbers that match the following examplesunique
- Determine if the field under validation is unique in a given database table.
Please refer to Wixel/GUMP documentation for more available validation rules.