Class for validating strings (parameters) in the application
Available Methods
setString: string, string - Prepare string for validations setFilters: string OR array - filters for sanitize setComplexParam: string OR array - Prepare name param for complex validation addValidators: string OR array - Collect validators validate: return void - Prepare And Run Sanitize setArrayValidation: array, array options - Prepare array for validations complexValidation: return void - Prepare And Run Sanitize For complex parameters getMessages: return array isValid: return bool
Validates can be PresenceOf, Alnum, Alpha, Date, Email, StringLength
Example #1
$validator = new Validator; $validator->setString('STRING_FOR_VALIDATION', 'name') ->setFilters([ 'striptags', 'string', ]) ->addValidators([ $validator->PresenceOf(['message'=>'YOUR_MESSAGE']) ]) ->validate();
Or how we can use complex validation (sanitizing)
Input array
$options = [ 'exclude'=>[ 'YOUR_PARAM','YOUR_PARAM ] ];
Example Complex Validating
$validator->setArrayValidation($this->request->getPost(), $options) ->setFilters([ 'YOYR_NAME_VARIABLE'=>[ 'striptags', 'int' ], etc... ]) ->addValidators([ 'YOUR_NAME_VARIABLE'=>[ $validator->setComplexParam('YOUR_NAME_VARIABLE'), $validator->PresenceOf(['message'=>'YOUR_MESSAGE']), $validator->Date(['message'=>'YOUR_MESSAGE', 'format'=>'Y-m-d']) etc... ], etc... ]) ->complexValidation();
For check if all variable are valid need to be execute
$validator->isValid();
if string or array not valid
$validator->getMessages();
Example
if($validator->isValid() === false) { $errors = $validator->getMessages(); }