zendframework / zend-inputfilter

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

CollectionInputFilter throws exception on scalar input #127

Closed autowp closed 7 years ago

autowp commented 7 years ago
    public function testCollectionInputFilterMustNotThrowsExceptionOnScalar()
    {
        $inputFilterManager = $this->getApplicationServiceLocator()->get('InputFilterManager');

        $factory = new \Zend\InputFilter\Factory($inputFilterManager);
        $inputFilter = $factory->createInputFilter([
            'type' => \Zend\InputFilter\CollectionInputFilter::class,
        ]);

        $inputFilter->setData('asd'); // throws Zend\InputFilter\Exception\InvalidArgumentException: Zend\InputFilter\CollectionInputFilter::setData expects an array or Traversable collection; invalid collection of type string provided
    }

That breaks to use input-filter especially where used nested collection filters.

weierophinney commented 7 years ago

@autowp That's expected behavior. Collections require that the incoming data is an array or traversable. If you're seeing that, it means the client-side code is submitting something that the input filter cannot deal with. As such, you have two options:

autowp commented 7 years ago

Ok. But that looks not symmetrical to scalar validations like StringLength where wrong type never follow to Exception (->isValid([]))

weierophinney commented 7 years ago

Validation is a separate subsystem, and independent from the input filters. Additionally, in this case, the exception happens before any validation is even attempted; it's thrown precisely because the value for would likely lead to serious errors further down the stack, including hydrators, filters, and validators.