0

FormType イベントリスナー内にボタンを設定したいのですが、そうすると次のエラーが発生します。

オプションauto_initializeが存在しません。既知のオプションは次のattrとおりblock_nameです。disabledlabeltranslation_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()
            ));
        }
[...]
}

今、私はもっと混乱しています。

ありがとうございます

4

1 に答える 1