0

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?

OCHnITRI
  • 5
  • 2

1 Answers1

1

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);
}
Sergio Rinaudo
  • 2,303
  • 15
  • 20
  • Okay, this is a start, but how can I use the data now in the filter? I would like to use a value from `$data` to set a `'required'` true or false. – OCHnITRI Jun 03 '21 at 14:11
  • In your init method you should have all your element declaration, with all their filters and validators, so you can get a defined element and edit its requirements depending on input data. If you have to add a new element, you can also do that. I've updated my answer – Sergio Rinaudo Jun 03 '21 at 14:35