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.

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.

Last updated