zendframework / zend-inputfilter

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

Can't create optional input #6

Closed svycka closed 9 years ago

svycka commented 9 years ago

I need an optional input lets say I expect data like {name: "some name", age: 18} or {name: "some name"} but don't want {name: "some name" age: null} and this is impossible to do now or I can't find a way how to do it. As an example I wrote a test for this, of course this does not pass:

class OptionalInputTest extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider dataProvider
     */
    public function testNewFeature($data, $isValid)
    {
        $inputFilter = new \Zend\InputFilter\InputFilter();
        $inputFilter->add([
            'name' => 'foo',
            'required' => false,
            'allow_empty' => false,
            'continue_if_empty' => true,
            'validators' => [
                ['name' => 'StringLength', 'options' => ['min' => 2]],
            ],
        ]);

        $inputFilter->setData($data);

        if ($isValid) {
            $this->assertTrue($inputFilter->isValid());
        } else {
            $this->assertFalse($inputFilter->isValid());
        }
    }

    public function dataProvider()
    {
        return [
            [['foo' => null], false],
            [['foo' => ''], false],
            [[], true],
            [['foo' => "bar"], true]
        ];
    }
}

Maybe I overthinking this?

weierophinney commented 9 years ago

What you have there is correct. When I ran the above, every one of those passed except the third data set, [[], true]. This makes sense, because:

In this case, absence of the value is the same as having a null value when you validate the set.

svycka commented 9 years ago

I understand that this is almost the same but for me it is BC because before this https://github.com/zendframework/zend-inputfilter/issues/4 it was possible to do this. And now I will have to move this check from input filter to maybe hydrator because:

// when ['foo' => null]
$filter->getData(); // returns ['foo' => null]
// when ['foo' => '']
$filter->getData(); // returns ['foo' => '']
// when []
$filter->getData(); // returns []

they all are considred empty and when I pass this data to hydrator null and '' are run by hydrator but last one is ignored. @weierophinney how could I solve this with inputfilter?