Question

Currently setting up a form using ZF2. I'd like to be able to setup validation for the form based on comparing the values of 2 different elements on the form. Where in the past I've used the Callback validator to pass in all of the forms values, it seems in my current scenario I can only get the value of the element being validated.

Here is how I setup my form

    public function __construct(Di $di)
{
    $this->setDi($di);
    parent::__construct();

    $inputFilter = $this->getInputFilter();

    $this->addElement(
        DateSelect::class,
        [
            'label' => 'Purchase Date'
        ], [
            'name' => 'purchase_date',
            'required' => 'true'
    ]);

    $this->addElement(
        Select::class,
        [
            'label' => 'What card type did you use to make the purchase',
            'value_options' => [
                    '' => '',
                    'credit' => 'Credit',
                    'debit' => 'Debit'
            ]
        ],[
            'required' => true,
            'name' => 'card_type',
            'helpText' => 'card type'
    ]);

    $this->addElement(Submit::class, [],['value' => 'Claim']);

    $inputFilter->add([
            'name' => 'card_type',
            'validators' => array(
                array(
                    'name' => Callback::class,
                    'options' => array(
                        'messages' => array(
                            Callback::INVALID_VALUE => 'Item must have been purchased in the last 6 years in order to claim',
                        ),
                        'callback' => function($value, $context = []) {
                                var_dump(func_get_args());

                                //I need to be able to get both card_type and purchase_date inputs here
                                die();
                            },
                    )
                )
            )
        ]);

}

The problem is that currently var_dump(func_get_args()) is currently only returning:

array(2) { [0]=> string(6) "credit" [1]=> array(1) { ["card_type"]=> string(6) "credit" } }

In the past I would have expected this to also pass in the purchase_date value as a part of the second argument of the callback.

Has anyone experienced this problem before? I'm using the callback function as expected in other areas of my application but can't seem to get it working here.

Thanks in advance

For clarification(although I don't believe it will have any effect on validation), the function $this->addElement is my own function, held on a trait and is simply:

public function addElement($class, $options, $attributes)
{
    $this->add(
        $this->getDi()->newInstance($class, ['options' => $options])->setAttributes($attributes)
    );
}
Était-ce utile?

La solution

Turns out that the callback will only get passed other values which the input filter is aware of.

The solution to this is:

$filter->add(['name' => 'card_type', 'required' => true]);

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top