首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向Symfony2传递附加参数的问题形成$options格式

向Symfony2传递附加参数的问题形成$options格式
EN

Stack Overflow用户
提问于 2014-12-30 22:32:37
回答 2查看 2.4K关注 0票数 2

我试图在Symfony 2.6.1中向我的表单类型传递一个额外的选项,如下(FabricanteForm.php)所示:

代码语言:javascript
运行
复制
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
            ->add('nombre', 'text')
            ->add('direccion', 'textarea')
            ->add('telefono', 'text', array(
                'required' => TRUE,
                'trim' => TRUE,
            ))
            ->add('fax', 'text', array(
                'required' => FALSE,
            ))
            ->add('correo', 'email', array(
                'required' => FALSE,
            ));

    if ($options['isFabricante'] !== null)
    {
        $builder->add('pais', 'text');
    }
    else
    {
        $builder->add('pais', 'entity', array(
            'class' => 'AppBundle:Pais',
            'property' => 'nombre',
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('qb')
                          ->where('qb.activo = :activoValue')
                          ->setParameter('activoValue', true);
            },
            'mapped' => FALSE,
            'expanded' => FALSE,
            'multiple' => TRUE,
        ));
    }
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setOptional(array(
        'isFabricante',
    ));

    $resolver->setDefaults(array(
        'data_class' => 'Sencamer\AppBundle\Entity\FabricanteDistribuidor',
        'intention' => 'fabricante',
        'isFabricante' => null
    ));
}

然后,我在控制器上创建如下表单:

代码语言:javascript
运行
复制
$entityPais = new Entity\Pais();
$formPaisesDistribuidor = $this->createForm(new Form\PaisesForm(), $entityPais, array('isFabricante' => null));
$formPaisesFabricante = $this->createForm(new Form\PaisesForm(), $entityPais, array('isFabricante' => true));

但我发现了一个错误:

代码语言:javascript
运行
复制
The option "isFabricante" does not exist. Known options are: "action", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "validation_groups", "virtual".

这是在Form Type上设置额外参数的正确方法吗?这是重用表单的最佳方式吗?(您可能会注意到,$formPaisesDistribuidor$formPaisesFabricante之间唯一的区别是pais字段类型,第一个是实体,第二个只是文本)

有什么帮助吗?建议?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-31 05:15:13

引用Turdaliev关于$options不被用于他想要的东西的观点--我不同意,你绝对可以用它。Symfony的文档展示了这两种方式。

下面是将实体管理器传递给options数组的Symfony示例:

using#使用-the-转换器

下面是另一个Symfony示例,将选项传递给构造函数:

type.html#creating-your-field-type-as-a-service

查看Symfony形成最佳实践,它们不会告诉您首选哪种方法,因此您可以自己决定。

我喜欢将$options数组用于简单的布尔flags...since --它们是选项。另外,如果您有几个字段,其中一些是可选的,另一些是必需的,则不需要处理构造函数参数排序问题。

至于你为什么会收到这个错误,我不完全确定。在Symfony 2.6中,setOptional()现在是setDefined()。您仍然可以使用旧的函数,但它是不推荐的,并将在3.0中删除。此外,如果只传递一个选项,则不再需要传递数组。您也不必在setDefaultOptions()中设置它

试试这个:

代码语言:javascript
运行
复制
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefined('isFabricante');

    $resolver->setDefaults(array(
        'data_class' => 'Sencamer\AppBundle\Entity\FabricanteDistribuidor',
        'intention'  => 'fabricante',
    ));
}
票数 2
EN

Stack Overflow用户

发布于 2014-12-31 01:33:37

正如您从异常中看到的那样,$options参数不用于您所使用的对象。因此,不必将自定义选项传递给Symfony的标准$options参数,您可以为FabricanteForm类创建自定义构造函数。以下是如何进行所有这些工作的示范:

PraisesType

代码语言:javascript
运行
复制
namespace School\CoreBundle\Form\Type;


use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class PraisesType extends AbstractType
{

private $options;

public function __construct($options)
{
    $this->options = $options;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
        $builder
            ->add('nombre', 'text')
            ->add('direccion', 'textarea')
            ->add('telefono', 'text', array(
            'required' => TRUE,
                'trim' => TRUE,
            ))
            ->add('fax', 'text', array(
                'required' => FALSE,
            ))
            ->add('correo', 'email', array(
                'required' => FALSE,
            ));

        if ($this->options['isFabricante'] !== null)
        {
            $builder->add('pais', 'text');
        }
        else
        {
            $builder->add('pais', 'entity', array(
                'class' => 'AppBundle:Pais',
                'property' => 'nombre',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('qb')
                        ->where('qb.activo = :activoValue')
                        ->setParameter('activoValue', true);
                },
                'mapped' => FALSE,
                'expanded' => FALSE,
                'multiple' => TRUE,
            ));
        }
    }


    public function getName()
    {
        return 'praises';
    }
}

使用

代码语言:javascript
运行
复制
    $entityPais = new Entity\Pais();
    $formPaisesDistribuidor = $this->createForm(new Form\PaisesType(array('isFabricante' => null)), $entityPais);
    $formPaisesFabricante = $this->createForm(new Form\PaisesType(array('isFabricante' => true)), $entityPais);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27713537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档