zendframework / zf2-documentation

Zend Framework 2 documentation sources
BSD 3-Clause "New" or "Revised" License
190 stars 571 forks source link

Correct function name #1524

Open pittyplatsch opened 8 years ago

manuakasam commented 8 years ago

The issue is that both setOptions() and setCallbackOptions() are valid functions, each with their own usecase:

$validator = new Callback();
$validator->setOptions([
    'callback' => function($args) {},
    'callbackOptions' => [
        'foo' => 'bar'
    ]
]);

or:

$validator = new Callback(function($args){});
$validator->setCallbackOptions([
    'foo' => 'bar'
]);

This is outlined in the sentence that you may have overread:

To pass them to the constructor, you would need to pass an array containing two keys, 
"callback" and "callbackOptions"
pittyplatsch commented 8 years ago

Yeah, one could potentially use setOptions() to set the 'callbackOptions' but especially considering the given examples (where the 'callback' is set in the constructor) I'm sure the original intention was to use setCallbackOptions(), otherwise $options would be not the same in the three examples. It also saves the need for a nested array (['callbackOptions' => […]), so is imho cleaner and explaining a validator specific function in the right place.

On another note: the associative array in your examples will work, but since the arguments are later merged to be used with call_user_func_array() there's no need to define any indexes. Also you're not using them in your callback function.

weierophinney commented 8 years ago

One rationale for keeping the docs as-is is that setOptions() is used internally by the constructor, and thus represents the options that would be set in module or application configuration for a validator instance.

Leaving this open for the time being.

pittyplatsch commented 8 years ago

I unfortunately don't know anything about module or application configuration as I'm using the component outside the framework – is it very different from using the InputFilter\Factory where callbackOption needs to be set if one wants to use it?

$factory = new \Zend\InputFilter\Factory();
$inputFilter = $factory->createInputFilter(array(
    'password' => [
        'name' => 'password',
        'required' => true,
        'validators' => [
            [
                'name' => 'callback',
                'options' => [
                    'callback' => function($value, $context, $option1) { return $option1; },
                    'callbackOptions' => [
                        false
                    ],
                ],
            ],
        ],
    ],
));

$inputFilter->setData(['password' => 'p@s$w0rd']);
echo $inputFilter->isValid() ? "Valid form" : "Invalid form";