根据环境在Symfony 3表单类型中添加或删除字段

时间:2016-08-02 15:44:58

标签: forms symfony

在我的网站上,人们可以在我的帖子上发表评论,我可以从我的后台回复。

我希望人们填写前台的recaptcha,所以我使用了EWZRecaptchaBundle,并做了以下CommentaireType

<?php

namespace Babdelaura\BlogBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;

class CommentaireType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('auteur', TextType::class)
            ->add('email', EmailType::class, array('required' => false))
            ->add('site', TextType::class, array('required' => false))
            ->add('contenu', TextareaType::class)
            ->add('recaptcha', EWZRecaptchaType::class, array(
                'mapped'      => false,
                'constraints' => array(new RecaptchaTrue())
            ))
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Babdelaura\BlogBundle\Entity\Commentaire'
        ));
    }

    /**
     * @return string
     */
    public function getBlockPrefix()
    {
        return 'babdelaura_blogbundle_commentaire';
    }
}

但是现在,我的后台办公室也收到了回收,而且我不想要它。

在Symfony 3和FQCN之前我在后台办公室做new CommentaireType(true)的类型,检索CommentaireType::__construct中的布尔值,如果是true则添加recaptcha。但现在我不能再这样做了。

有人有一个干净的解决方案来处理这种情况吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以在调用服务时使用options来传递布尔值,例如:

$form = $this->createForm(new CommentaireType(), $commentaire, array(
        'recaptcha' => false,
));

然后在你的CommentaireType:

class CommentaireType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('auteur', TextType::class)
            ->add('email', EmailType::class, array('required' => false))
            ->add('site', TextType::class, array('required' => false))
            ->add('contenu', TextareaType::class);
        if($options['recaptcha']) {
           $builder
            ->add('recaptcha', EWZRecaptchaType::class, array(
                'mapped'      => false,
                'constraints' => array(new RecaptchaTrue())
            ));
        }
     }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Babdelaura\BlogBundle\Entity\Commentaire',
            'recaptcha' => false,
        ));
    }

    /**
     * @return string
     */
    public function getBlockPrefix()
    {
        return 'babdelaura_blogbundle_commentaire';
    }
}