Php 设置buildForm的可选参数

Php 设置buildForm的可选参数,php,symfony,symfony-forms,Php,Symfony,Symfony Forms,我正在使用选项resolverinterface向表单传递一些额外参数。这是表单的代码: class OrdersType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { if ($options['curr_action'] !== NULL) { $builder

我正在使用
选项resolverinterface
向表单传递一些额外参数。这是表单的代码:

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';
    }

}
这就是我在控制器上构建表单的方式:

$order = new Orders();
$orderForm = $this->createForm(new OrdersType(), $order, array('action' => $this->generateUrl('save_order'), 'register_type' => $type));
但我得到了一个错误:

注意:未定义索引:中的当前操作 /var/www/html/tanane/src/tanane/FrontendBundle/Form/Type/OrdersType.php 第95行

为什么??当此代码设置时,
curr\u action
是否为
$options
格式中的可选操作

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

没错。PHP在访问未知数组键时触发
注意

要正确处理此问题,您有两种解决方案:

**1) 替换:
if($options['curr\u action']!==NULL)

使用
if(数组键存在('curr\u action',$options)&&$options['curr\u action']!==NULL)

有点麻烦,但它能工作

2)另一种解决方案是只定义默认值:

$resolver->setDefaults(array(
        'data_class' => 'Tanane\FrontendBundle\Entity\Orders',
        'render_fieldset' => FALSE,
        'show_legend' => FALSE,
        'intention' => 'orders_form',
        'curr_action' => NULL // <--- THIS
    ));
$resolver->setDefaults(数组)(
“数据类”=>“Tanane\FrontendBundle\Entity\Orders”,
“render_fieldset”=>FALSE,
'show_legend'=>FALSE,
“意向”=>“订单形式”,

'curr\u action'=>NULL//如果这有助于
public function\u构造($options=array()){$this->options=$options;}
在表单类型中传递变量,以及
$orderForm=$this->createForm(new OrdersType($options)..
在您的表单creationI认为这是您在$orderForm=$this->createForm(new OrdersType(),$order,array('action'=>$this->generateUrl('save_order'),'register_type'=>$type')中编写了“action”的错误;在这里更改curr_action或在OrdersType.php中更改为action。您可以遵循构造函数的@NawfalSerrar建议