1
我想提交FormType設置按鈕的FormType事件監聽器裏,但是當我這樣做,我得到這個錯誤:Symfony的2.3事件監聽錯誤
選項auto_initialize
不存在。已知的選項有:attr
,block_name
,disabled
,label
,translation_domain
如果我設置了事件監聽它的作品的。有人能幫我嗎?
class ClientType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('enquirer', new ContactType(), array(
"by_reference" => true,
"required" => true,
'validation_groups' => array('required'),
'details_field' => true,
))
[...]
;
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
$client=$event->getData();
$form=$event->getForm();
if($client && !$client->getId()){
$form->add("createBtn", "submit", array("label"=>"Create", "attr"=>array("class"=> "btn btn-primary")));
} else {
$form->add("saveBtn", "submit", array("label"=>"Save", "attr"=>array("class"=> "btn btn-primary")));
}
});
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Sme\ClientBundle\Entity\Client',
));
}
public function getName()
{
return 'client_form';
}
}
堆棧跟蹤: 我沒有設置 「auto_initialize」,但裏面FormFactory它。有任何想法嗎?
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 47 +
at FormFactory ->createNamed ('createBtn', 'submit', null, array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false))
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 819 +
at Form ->add ('createBtn', 'submit', array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary')))
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\src\Sme\ClientBundle\Form\ClientType.php at line 117
完整的堆棧跟蹤
at OptionsResolver ->validateOptionsExistence (array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false))
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\OptionsResolver\OptionsResolver.php at line 219 +
at OptionsResolver ->resolve (array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false))
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\ResolvedFormType.php at line 109 +
at ResolvedFormType ->createBuilder (object(FormFactory), 'createBtn', array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false))
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 87 +
at FormFactory ->createNamedBuilder ('createBtn', 'submit', null, array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false))
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\FormFactory.php at line 47 +
at FormFactory ->createNamed ('createBtn', 'submit', null, array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary'), 'auto_initialize' => false))
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\vendor\symfony\symfony\src\Symfony\Component\Form\Form.php at line 819 +
at Form ->add ('createBtn', 'submit', array('label' => 'Create', 'attr' => array('class' => 'btn btn-primary')))
in C:\Users\albert\Dropbox\smeprojects\brightcare\brightcare-dev\src\Sme\ClientBundle\Form\ClientType.php at line 117
看着\供應商\ symfony的\ symfony中的\ src \的Symfony \分量\表格\ form.php的我發現代碼:
public function add($child, $type = null, array $options = array())
{
[...]
if (!$child instanceof FormInterface) {
if (!is_string($child) && !is_int($child)) {
throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormInterface');
}
if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
}
// Never initialize child forms automatically
$options['auto_initialize'] = false;
if (null === $type) {
$child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass(), $child, null, $options);
} else {
$child = $this->config->getFormFactory()->createNamed($child, $type, null, $options);
}
} elseif ($child->getConfig()->getAutoInitialize()) {
throw new RuntimeException(sprintf(
'Automatic initialization is only supported on root forms. You '.
'should set the "auto_initialize" option to false on the field "%s".',
$child->getName()
));
}
[...]
}
現在我更困惑。
感謝您
那麼你的問題是什麼?你的表單正在工作,對吧? 「auto_initialize」選項似乎只適用於根形式,而不適用於事件偵聽器。 –
我的問題是,我需要設置事件監聽器中的按鈕,並在那裏它劑量工作 – albert
你可以嘗試'PRE_SET_DATA'?它適用於我的一個項目,其代碼類似於您的項目。 –