Access the set data for logic in Laminas filter?

343 Views Asked by At

I am using Mezzio/Laminas (newer version of Zend) and want to check whether an id is less than a number. If it is not, another form element is required. Is it possible to access the set data in a laminas-filter in any way?

1

There are 1 best solutions below

2
On BEST ANSWER

In your input filter class, you can override setData

public function setData($data)
{
    // your custom logic here

    return parent::setData($data);
}

In the example below I added an example of how to edit an already defined input or add a new input definition

public function setData($data)
{
    $type = array_key_exists('type', $data) ? $data['type'] : null;

    switch($type) {
        case 'CASE_ONE':
            $this->get('someAlreadyDefinedElement')->setRequired(false);
            $this->add([
                'name'       => 'someOtherElement',
                'required'   => true,
                'validators' => [
                    [
                        'name'    => 'Date',
                        'options' => [
                            'format' => \DateTime::ISO8601,
                        ],
                    ],
                ],
            ]);
        break;
        case 'SOME_OTHER_CASE':
           // some other logic
        break;
    }

    return parent::setData($data);
}