Change Fieldset Fields' required parameter dynamically

大兔子大兔子 提交于 2019-12-06 02:08:07

问题


I have a moneyFieldset with 2 fields, amount and currency.

class MoneyFieldset ...
{
public function __construct($name = null, $options = array())
    {
        parent::__construct($name, $options);
        $this->setHydrator(...);

        $this->add(array(
            'name'    => 'currency',
            'type'    => 'select',
            'options' => array(
                'value_options' => \Core\Service\Money::getAvailableCurrencies(true),
            ),
            'attributes' => array(
                'value' => \Core\Service\Money::DEFAULT_CURRENCY,
            ),
        ));

        $this->add(array(
            'name'       => 'amount',
            'type'       => 'text',
        ));
    }
}
public function getInputFilterSpecification()
    {
        $default = [
            'amount' => [
                'required'    => false,
                'allow_empty' => true,
                'filters'     => [
                    ['name' => AmountFilter::class]
                ],
                'validators' => [
                ]
            ],
            'currency' => [
                'required'    => false,
                'allow_empty' => true,
                'filters'     => [
                    ['name' => StringToUpper::class]
                ],
                'validators' => [
                ]
            ]
        ];
        return \Zend\Stdlib\ArrayUtils::merge($default, $this->filterSpec, true);
    }

I'm using moneyFieldset in my other fieldsets like this:

        // Price Field
        $this->add(array(
            'name'       => 'price',
            'type'       => 'form.fieldset.moneyFieldset',
            'attributes' => array(
                'required'    => true,
                'invalidText' => 'Please type an amount'
            ),
            'options' => array(
                ...
            ),
        ));

When I set filter like this:

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

It's not working because price has 2 fields, so how can I say price[amount] and price[curreny] is required?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/34461683/change-fieldset-fields-required-parameter-dynamically

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