Change Fieldset Fields' required parameter dynamically
Asked Answered
B

2

5

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?

Backdate answered 25/12, 2015 at 9:31 Comment(0)
F
4

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.

Flunky answered 25/12, 2015 at 12:24 Comment(5)
I've tried it but nothing changed. This is the output : <input type="text" name="list[price][amount]" class="form-control" value="">. There is no required='required' parameter.Backdate
With zf2, say in inputFilterSpecification that a field or a group of field is required, not means that a attribute required is set in HTML, it's just for validation when you do $form->isValid(), it will check all required fields within the validationGroupSealey
So I also need to set required attribute, too. I thought that price[attributes][required]=true will produce required=required HTML attributeBackdate
Yes it's something like that.Sealey
Awarded you a bounty because of meta.#314169Spancel
S
3

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.

Sealey answered 29/12, 2015 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.