简体   繁体   中英

Access the set data for logic in Laminas filter?

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM