TL;DR: I know, there is already an Validation Module. And it wasn't my intend to build another module. But I needed it in so many places which lead to duplicate code and suddenly there was a module Perhaps it's useful to someone. At the moment there are only validators I need in my current project, if you miss one, please tell me or (preferred) send a pull request! ------ This module provides a set of useful validation methods using ProcessWire sanitizers. Example Usage:
$conf = array(
'username' => array('isEmpty', 'isUnique' => array('ident' => 'name', 'sanitize' => 'username')),
'pass' => array('range' => array('min' => 6, 'max' => 20)),
'pass_confirm' => array('isEqual' => array('equal' => 'pass'))
);
$validator = new Validator;
$validator->setConfig($conf);
if (!$validator->isValid()) $errors = $validator->getErrors();
You can also use it to validate POST requests:
$validator = new Validator;
$validator->setConfig($validatorConf);
// validation failed
if (!$validator->isValid()) {
$data = array(
'success' => false,
'errors' => $validator->getMessages() // $validator->getErrors()
);
$this->returnJson($data, 400);
}
❯ http -f POST http://pw.dev/v1/user/ email=info@com username=exampleUser pass=short
{
"errors": {
"email": [
"Please enter a valid email address."
],
"pass": [
"This field must be at least '6' characters.",
"'short' must contain at least one digit character"
]
},
"success": false
}
For a detailed documentation please have a look at the guides:
Installation
Usage
Available Validators
Error Messages
phpunit Testing