我想在FormType事件监听器中设置按钮,但是当我这样做时,我得到了错误:
选项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
查看\ vendor \ symfony \ symfony \ src \ Symfony \ Component \ Form \ 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()
));
}
[...]
}
现在我更加困惑。
谢谢你