gajus / vlad

Input validation library promoting succinct syntax with extendable validators and multilingual support.
Other
104 stars 8 forks source link

Alternative syntax #1

Closed gajus closed 10 years ago

gajus commented 10 years ago

I propose an alternative syntax:

[input_name1, input_name2, input_name3, input_name4] => [rule1[, opional_rule1, opional_rule2 param1=val1]].

This solves a known limitation of the current implementation:

    not_empty
    email
        email[input]

Where email rule is expected to be executed only if not_empty rule passes.

gajus commented 10 years ago

New syntax has been implemented.

$test = $vlad->test([
    [
        ['foo', 'bar', 'baz'],
        [
            ['fail' => 'break'],
            // Define Validator failure scenario:
            // * 'silent' exclude input from the current validator chain.
            // * 'soft' record an error and progress to the next Validator.
            // * 'hard' (default) record an error and exclude the selector from the rest of the Test.
            // * 'break' record an error and interrupt the Test.
            'required',
            'not_empty',
            ['fail' => 'hard'], // Reset default Validator failure scenario.
            'email',
            ['length', 'max' => 10] // Length Validator with constructor options.
        ]
    ],
    [
        ['qux', 'quux'],
        [
            // Validate email only if it is non-empty string.
            ['fail' => 'silent'],
            'not_empty',
            ['fail' => 'hard'],
            'email'
        ]
    ]
]);