简体   繁体   English

Symfony2-FormType中的条件

[英]Symfony2 - Condition in FormType

I have a class RegisterType for my form: 我的表单有一个RegisterType类:

<?php
class RegisterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
        $builder->add('price');        
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Register'
        ));
    }

    public function getName()
    {
        return 'register';
    }
}
?>

The Register Entity has a relation with an Entity called Product 注册实体与称为产品的实体有关系

I would like to add this condition to the buildForm function: 我想将此条件添加到buildForm函数:

<?php
if ($product->getTitle() == 'SuperProduct') {
    $builder->add('amount', 'money', [
        'required' => FALSE,
    ]);
}
?>

If the product title has the value 'SuperProduct' then I add a field in the the form. 如果产品标题的值为“ SuperProduct”,则在表单中添加一个字段。

But I have no idea about the syntax I have to use to call another Entity value in a FormType. 但是我不知道必须使用哪种语法在FormType中调用另一个Entity值。

Thanks in advance for your help. 在此先感谢您的帮助。

Ah good you've implemented your FormType as an independent class rather than in a controller, so what I'd do is pass in your product entity as an option to a private variable within your FormType class. 好的,您已经将FormType实现为独立类而不是在控制器中实现的,所以我要做的就是将您的产品实体作为选项传递给FormType类中的私有变量。 I'm not sure what class your product is or the name space it resides in, so you'll have to fill that in. 我不确定您的产品属于哪个类或产品所在的名称空间,因此您必须填写。

I've also written this in the expectation that you could have more than one option to pass through in the future, so it'll be useful to other FormType classes you write. 我也已经写过这篇文章,期望将来您可以有多个选项可以通过,因此它对您编写的其他FormType类很有用。 Each variable you define in the class scope can be set when you create an instance of the FormType class. 创建FormType类的实例时,可以设置在类范围内定义的每个变量。 Be sure to initialise these variables as false instead of null, or the code in the constructor function won't see them. 确保将这些变量初始化为false而不是null,否则构造函数中的代码将看不到它们。

<?php
    class RegisterType extends AbstractType
    {
        private $product = false;

        public function __construct(array $options = [])
        {
            foreach ($options as $name => $value) {
                if (isset($this->$name)) {
                    $this->$name => $value;
                }
            }
        }

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name')
                ->add('price')
            ;

            if ($this->product && is_a("\Namespace\To\Your\Entity\Called\Product", $this->product)) {
                if ($this->product->getTitle() == 'SuperProduct') {
                    $builder
                        ->add('amount', 'money', [
                            'required' => FALSE,
                        ])
                    ;
                }
            }
        }

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'AppBundle\Entity\Register'
            ));
        }

        public function getName()
        {
            return 'register';
        }
    }
?>

That "is_a()" function checks that the product variable passed in through the form type class constructor really is an instance of your product entity, so you can be sure that the getTitle() function exists. 该“ is_a()”函数检查通过表单类型类构造函数传入的产品变量是否确实是您的产品实体的实例,因此您可以确定存在getTitle()函数。

Now when you create a new instance of your RegisterType class, you need to pass through an options array including your product. 现在,当您创建RegisterType类的新实例时,您需要通过一个包含产品的options数组。 Two conceptual variable names for you to rename here. 您可以在此处重命名两个概念变量名称。

<?php
    $form = $this->createForm(new RegisterType([
        "product" => $instanceOfProductEntityOrNull,
    ]), $theEntityTheFormWillBeHandling);
?>

Try something like this: 尝试这样的事情:

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
    $product = $event->getData();
    $form = $event->getForm();

    if ($product->getTitle() == 'SuperProduct') {
        $form->add('name', 'text');
    }
});

And do not forget the specific use 并且不要忘记具体用途

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM