> For the complete documentation index, see [llms.txt](https://sw-fw-less.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sw-fw-less.gitbook.io/project/mvc/param-validator.md).

# Param Validator

### Cake Validation

```
$errors = (new \Cake\Validation\Validator())->requirePresence('key')
    ->lengthBetween('key', [1, 10])
    ->add('key', 'string', [
        'rule' => [\App\components\Validator::class, 'string'],
        'message' => 'key is not a string'
    ])->errors($params);
```

For detailed usage of cake validation, please see [document](https://book.cakephp.org/3.0/en/core-libraries/validation.html#adding-validation-providers).

### Phalcon Validation(Recommended)

Before using this validation, please install phalcon extension.

```
$messages = (new Validation())->add('key', new Validation\Validator\PresenceOf(['message' => 'key is required']))
    ->add('key', new Validation\Validator\StringLength(['min' => 1, 'max' => 10, 'message' => 'key is not between 1 t0 10']))
    ->add('key', new Validation\Validator\Callback([
        'callback' => function($data) {
            return \App\components\Validator::string($data['key'], $data);
        },
        'message' => 'key is not a string',
    ]))->validate($params);
$errors = [];
foreach ($messages as $message) {
    $errors[] = $message->getMessage();
}
```

For detailed usage of phalcon validation, please see [document](https://docs.phalconphp.com/en/3.4/validation).
