Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Fieldset Fields' required parameter dynamically

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?

like image 672
Eray Avatar asked Dec 25 '15 09:12

Eray


2 Answers

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.

like image 197
AlexP Avatar answered Sep 30 '22 05:09

AlexP


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.

like image 43
Greco Jonathan Avatar answered Sep 30 '22 06:09

Greco Jonathan