我试图在Symfony 2.6.1中向我的表单类型传递一个额外的选项,如下(FabricanteForm.php
)所示:
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
));
}
然后,我在控制器上创建如下表单:
$entityPais = new Entity\Pais();
$formPaisesDistribuidor = $this->createForm(new Form\PaisesForm(), $entityPais, array('isFabricante' => null));
$formPaisesFabricante = $this->createForm(new Form\PaisesForm(), $entityPais, array('isFabricante' => true));
但我发现了一个错误:
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
字段类型,第一个是实体,第二个只是文本)
有什么帮助吗?建议?
发布于 2014-12-30 21:15:13
引用Turdaliev关于$options不被用于他想要的东西的观点--我不同意,你绝对可以用它。Symfony的文档展示了这两种方式。
下面是将实体管理器传递给options数组的Symfony示例:
下面是另一个Symfony示例,将选项传递给构造函数:
type.html#creating-your-field-type-as-a-service
查看Symfony形成最佳实践,它们不会告诉您首选哪种方法,因此您可以自己决定。
我喜欢将$options
数组用于简单的布尔flags...since --它们是选项。另外,如果您有几个字段,其中一些是可选的,另一些是必需的,则不需要处理构造函数参数排序问题。
至于你为什么会收到这个错误,我不完全确定。在Symfony 2.6中,setOptional()
现在是setDefined()
。您仍然可以使用旧的函数,但它是不推荐的,并将在3.0中删除。此外,如果只传递一个选项,则不再需要传递数组。您也不必在setDefaultOptions()
中设置它
试试这个:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefined('isFabricante');
$resolver->setDefaults(array(
'data_class' => 'Sencamer\AppBundle\Entity\FabricanteDistribuidor',
'intention' => 'fabricante',
));
}
发布于 2014-12-30 17:33:37
正如您从异常中看到的那样,$options参数不用于您所使用的对象。因此,不必将自定义选项传递给Symfony的标准$options参数,您可以为FabricanteForm类创建自定义构造函数。以下是如何进行所有这些工作的示范:
PraisesType
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';
}
}
使用
$entityPais = new Entity\Pais();
$formPaisesDistribuidor = $this->createForm(new Form\PaisesType(array('isFabricante' => null)), $entityPais);
$formPaisesFabricante = $this->createForm(new Form\PaisesType(array('isFabricante' => true)), $entityPais);
https://stackoverflow.com/questions/27713537
复制相似问题