Secure Validator
, (*1)
, (*2)
Secure Validator is a library for input validation. It is based on Sirius Validation., (*3)
Requirements
Features
Default Rules
Secure Validator promotes strict validation. It sets default validation rules to all fields., (*4)
-
ValidUtf8 checks if value is valid UTF-8 character encoding
-
IsString checks if value is string
-
NoControl checks if value does not have control characters (except for tab and newline)
And, (*5)
That is you have to set (overwrite) max length rule to all fields. You don't forget it., (*6)
If a field does not match the default rules, you can remove the rules., (*7)
$validator->remove('field', 'ValidUtf8');
Fatal Rules
You can set a validation rule as fatal to detect abnormal input like an attack., (*8)
$validator->add('field', 'maxlength', ['max' => 60, 'fatal' => true]);
If a fatal rule fails, exception FatalValidationError will be thrown immediately., (*9)
Validated Data
You can get validated data only with $validator->getValidated()., (*10)
How to Use
See example.php and Sirius Validation Documentation., (*11)
$validator = new \Kenjis\Validation\Validator;
$validator->add('field', 'required | maxlength(max=60)');
if ($validator->validate($_POST)) {
// validation passed
} else {
// validation failed
}
See Built-in validation rules., (*12)
Added Method
Validator::filter()
Add filtering rule of Sirius\Filtration. See Built-in filters., (*13)
Validator will apply filters before validation., (*14)
$validator->filter('field', 'StringTrim');
Validator::getValidated()
Get validated values., (*15)
$allData = $validator->getValidated();
$field = $validator->getValidated('field');
Get input value after filtering of specific field., (*16)
$field = $validator->getInputValue('field');
Tips
When you set required, if a rule fails, Sirius Validation will not apply any more rules to that field., (*17)