zendframework / zend-inputfilter

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

BC in validators context value #16

Closed svycka closed 9 years ago

svycka commented 9 years ago

Before 2.5.2 context values was all inputs added to inputfilter but 2.5.2 changed this and now returns raw values passed to inputFilter. I Think this should be and is correct behavior, but unfortunately now it is BC. This should be at least mentioned in release notes or fixed.

example:

<?php

require __DIR__ . '/vendor/autoload.php';

class MyFilter extends Zend\InputFilter\InputFilter
{
    public function init()
    {
        $this->add([
            'name' => 'field1',
            'required' => true,
        ]);

        $this->add([
            'name' => 'field2',
            'required' => true,
            'validators' => [
                [
                    'name' => 'Zend\Validator\Callback',
                    'options'  => [
                        'callback' => function ($value, $context) {
                            var_dump($context);
                            return true;
                        },
                    ],
                ],
            ],
        ]);
    }
}

$app = Zend\Mvc\Application::init([
    'modules' => [
    ],

    'module_listener_options' => [
        'module_paths' => [
        ],

        'config_glob_paths' => [
        ],
    ],

    'input_filters' => [
        'invokables' => [
            'MyFilter' => 'MyFilter',
        ],
    ],
]);

$manager = $app->getServiceManager()->get('InputFilterManager');
$filter = $manager->get('MyFilter');

echo "\n";
echo "#------------------------------\n";
echo "# Test case\n";
echo "#------------------------------\n";
echo "\n";
$filter->setData([
    'field2' => 'foo',
]);

$filter->isValid();

output with versions =<2.5.1

#------------------------------
# Test case
#------------------------------

array(2) {
  'field1' =>
  NULL
  'field2' =>
  string(3) "foo"
}

output with versions =>2.5.2

#------------------------------
# Test case
#------------------------------

array(1) {
  'field2' =>
  string(3) "foo"
}
svycka commented 9 years ago

@Maks3w do you think this should be fixed or not? This holding me from updating. @weierophinney what do you think? It seems that your fix for another bug introduced this.

marcuswinkler commented 9 years ago

I just want to note that this was reverting behavior back to where it was in 2.2.4 and before. See zendframework/zf2#6264

So:

version result
<= 2.2.4 raw values get passed as context
>= 2.2.5 && <= 2.5.1 values extracted from inputs get passed as context
>= 2.5.2 raw values get passed as context
svycka commented 9 years ago

Even more fun :) then maybe time to take final decision on this and add some tests.

Maks3w commented 9 years ago

@svycka I don't understand your example. Your code does not use any of getXValue method. What method is giving you troubles?

svycka commented 9 years ago

@Maks3w problem is not the getXValue methods but the context passed to validators. It varies between versions. Example shows that when I have input filter with two inputs and I provide data for one input, as context in validators I don't get both values. But with older versions of zend-inputfilter there would be both in context.

//simplified example
$inputFilter = ['A', 'B']; // A, B inputs
$inputFilter->setData(['B' => null]);

then when validating B validators as context gets ['B' => null] when in older versions it would be ['A' => null, 'B' => null]

Also @marcuswinkler noted that this change happened not the first time.

Maks3w commented 9 years ago

Ahm. Seems I found the same issue today while reviewing the code. #24

Maks3w commented 9 years ago

@svycka Could you review #24