I'm trying to pass a extra option to my form type in Symfony 2.6.1 as follow (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
));
}
And then I create the form on controller as follow:
$entityPais = new Entity\Pais();
$formPaisesDistribuidor = $this->createForm(new Form\PaisesForm(), $entityPais, array('isFabricante' => null));
$formPaisesFabricante = $this->createForm(new Form\PaisesForm(), $entityPais, array('isFabricante' => true));
But I got this error:
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".
Is that the right way to set extra parameters on a Form Type? Is this the best way to reuse the form? (As you may notice the only difference between $formPaisesDistribuidor and $formPaisesFabricante is pais field type, first one is a entity and second one is just a text)
Any help? Advice?