Change Fieldset Fields' required parameter dynamically

泄露秘密 提交于 2019-12-04 08:08:31

You can provide the input specifications for the nested fieldset (within the context of the form) using the following array structure.

public function getInputFilterSpecification()
{
    return [
        // ...
        'price' => [
            'type' => 'Zend\InputFilter\InputFilter',
            'amount' => [
                'required' => true,
            ],
            'currency' => [
                'required' => true,
            ]
        ],
        //...
    ];
}

If you are dynamically modifying the values of the input filter it might be worth considering creating the validator using a service factory class and then attaching it to a form using the object API rather than arrays.

As I said in @AlexP's comment, a field, or a group of field declared as Required like this :

function getInputFilterSpecification()
    {
        'price' => [
            'required'    => true,
            'allow_empty' => false,
        ],
     }

Not means that it will be print an html like this :

<input type="text" required="required"/>

It just check when you'll do $form->isValid() if your fields are empty and required or other checks. To achieve that, you just have to set in attributes that you want to require those fields. As you already did. Attributes can add, same as class attribute, html code to an input.

P.S : AlexP's answer is the best answer I just give more info about it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!