0
我使用OptionsResolverInterface
將一些額外參數傳遞給我的表單。這是形式的代碼:將可選參數設置爲buildForm
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));
但我發現了這個錯誤:
Notice: Undefined index: curr_action in /var/www/html/tanane/src/Tanane/FrontendBundle/Form/Type/OrdersType.php line 95
爲什麼?此代碼設置是否不是curr_action
可選形式$options
?
$resolver->setOptional(array(
'curr_action'
));
我使用該構造來傳遞變量,如果這可以幫助'公共函數__construct($ options = array()){ $ this-> options = $ options;在你的表單中創建'$ orderForm = $ this-> createForm(new OrdersType($ options)....' – 2014-09-19 01:51:23
我認爲這是你在$ orderForm = $ this-> createForm(new OrdersType(),$ order,array('action'=> $ this-> generateUrl('save_order'),'register_type'=> $ type));更改此處curr_action或OrdersType中的更改。 PHP的行動。你可以按照@NawfalSerrar建設者的建議 – herr 2014-09-19 06:22:35