ReynierPM
ReynierPM

Reputation: 18660

Setup optional parameters to buildForm

I'm passing some extra parameters to my form using OptionsResolverInterface. This is the code for the form:

class OrdersType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if ($options['curr_action'] !== NULL)
        {
            $builder
                    ->add('status', 'choice', array(
                        'choices' => array("Pendiente", "Leido"),
                        'required' => TRUE,
                        'label' => FALSE,
                        'mapped' => FALSE
                    ))
                    ->add('invoice_no', 'text', array(
                        'required' => TRUE,
                        'label' => FALSE,
                        'trim' => TRUE
                    ))
                    ->add('shipment_no', 'text', array(
                        'required' => TRUE,
                        'label' => FALSE,
                        'trim' => TRUE
            ));
        }

        if ($options['register_type'] == "natural")
        {
            $builder->add('person', new NaturalPersonType(), array('label' => FALSE));
        }
        elseif ($options['register_type'] == "legal")
        {
            $builder->add('person', new LegalPersonType(), array('label' => FALSE));
        }
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setRequired(array(
            'register_type'
        ));

        $resolver->setOptional(array(
            'curr_action'
        ));

        $resolver->setDefaults(array(
            'data_class' => 'Tanane\FrontendBundle\Entity\Orders',
            'render_fieldset' => FALSE,
            'show_legend' => FALSE,
            'intention' => 'orders_form'
        ));
    }

    public function getName()
    {
        return 'orders';
    }

}

And this is how I' building the form at controller:

$order = new Orders();
$orderForm = $this->createForm(new OrdersType(), $order, array('action' => $this->generateUrl('save_order'), 'register_type' => $type));

But I'm getting this error:

Notice: Undefined index: curr_action in /var/www/html/tanane/src/Tanane/FrontendBundle/Form/Type/OrdersType.php line 95

Why? Is not curr_action a optional in the form $options as this code sets?

$resolver->setOptional(array(
   'curr_action'
));

Upvotes: 0

Views: 1140

Answers (1)

Jovan Perovic
Jovan Perovic

Reputation: 20191

Exactly. PHP fires NOTICE when accessing unknown array key.

To properly handle this, you have 2 solutions:

**1) Replace: if ($options['curr_action'] !== NULL)

with if (array_key_exists('curr_action', $options) && $options['curr_action'] !== NULL)

A bit cumbersome but it works...

2) Another solution would be to just define default value:

$resolver->setDefaults(array(
        'data_class' => 'Tanane\FrontendBundle\Entity\Orders',
        'render_fieldset' => FALSE,
        'show_legend' => FALSE,
        'intention' => 'orders_form',
        'curr_action' => NULL // <--- THIS
    ));

Upvotes: 1

Related Questions