zendframework / zend-inputfilter

InputFilter component from Zend Framework
BSD 3-Clause "New" or "Revised" License
64 stars 50 forks source link

Add validator's priority retrieval into Factory #180

Closed mtagliab closed 4 years ago

mtagliab commented 5 years ago

I noticed that it was impossible to set validator's priority using the "array notation", even if this feature was already there as it already exists for filters (see line 384 of Factory.php).

To add the priority, it will be enough to add the priority key in the array:

$inputFilter[] = [
    'name' => 'text_elt',
    'required' => true,
    'filters' => [
        ['name' => 'StripTags'],
        ['name' => 'StringTrim']
    ],
    'validators' => [
        [
            'name' => 'StringLength',
            'options' => [
                'max' => 256
            ],
            'priority' => 2
        ]
    ]
];

FactoryTest fix

I also noticed that the FactoryTest class had a minor issue, which was that validators where not tested inside the testFactoryWillCreateInputWithSuggestedValidators method. I fixed the loop and added a final assert to verify that all the validators have been tested.

froschdesign commented 4 years ago

@webimpress I think an unit test is missing here. Like this one:

https://github.com/zendframework/zend-inputfilter/blob/a23e67d41de09bbc325543acb756ba448f182c20/test/FactoryTest.php#L685-L726

michalbundyra commented 4 years ago

@froschdesign test added. Would you mind to have a look on it, please?

It's a bit tricky as I mentioned in the commit comment - we can't use getValidators on ValidatorChain, as the result is not sorted by priority. When validators are called (in isValid) these are ordered, and this is how I check it.

froschdesign commented 4 years ago

@webimpress

When validators are called (in isValid) these are ordered, and this is how I check it.

👍

michalbundyra commented 4 years ago

Thanks, @mtagliab!