vekah
vekah

Reputation: 990

symfony2 form abstract type send options

I am doing some forms under Symfony 2.1.9 For useful reasons, I have done a FUser class, who extends from my user entity. I have done an UserType class, for being able to reuse my form.

My UserType class looks like this :

<?php
namespace CD\BoutiqueBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
  $builder
        ->add('nom', 'text')
        ->add('prenom', 'text')
        ->add('mail', 'text')
        ->add('fb_id','hidden',array('data'=>'null'))
        ->add('pass', 'repeated', 
        array('type' => 'password', 'invalid_message' => 'The password fields must match.',
        'first_options'  => array('label' => 'Mot de passe'),
        'second_options' => array('label' => 'Confirmer le mot de passe')))
        ->add('tel', 'text', array('required' => false))
        ->add('adresse', 'text')
        ->add('code_postal', 'text')
        ->add('ville', 'text')
        ->add('tel', 'text')
        ->add('date_naissance', 'birthday', array('widget' => 'choice', 'years' => range(1933,2013,1), 'months' => range(1,12,1), 'days' => range(1,31,1)));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'CD\ConfigBundle\Entity\User'
    ));
}
public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'CD\ConfigBundle\Entity\User',
    );
}

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

What I want to do is quite simple, but I didn't find my answer neither on google, neither on symfony doc, neither on this website. I want to send options from my controller to this UserType.

For instance, in some cases, I don't want to add "tel" and "adresse" inputs. Or sometimes, I want this input to be required; in others, I want the same input not required.

How can I do this?

Upvotes: 0

Views: 3011

Answers (1)

seltzlab
seltzlab

Reputation: 340

If I understand correctly you need to modify your form dynamically.

Take a look here: How to Dynamically Modify Forms Using Form Events

UPDATE

To pass options to your form you can modify the form this way:

<?php
namespace CD\BoutiqueBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
  $myCustomVar = $options['foo'];

  $builder
    ->add('nom', 'text')
    ->add('prenom', 'text')
    ->add('mail', 'text')
    ->add('fb_id','hidden',array('data'=>'null'))
    ->add('pass', 'repeated', 
    array('type' => 'password', 'invalid_message' => 'The password fields must match.',
    'first_options'  => array('label' => 'Mot de passe'),
    'second_options' => array('label' => 'Confirmer le mot de passe')))
    ->add('tel', 'text', array('required' => false))
    ->add('adresse', 'text')
    ->add('code_postal', 'text')
    ->add('ville', 'text')
    ->add('tel', 'text')
    ->add('date_naissance', 'birthday', array('widget' => 'choice', 'years' => range(1933,2013,1), 'months' => range(1,12,1), 'days' => range(1,31,1)));
}

 public function setDefaultOptions(OptionsResolverInterface $resolver)
 {
  $resolver->setDefaults(array(
    'data_class' => 'CD\ConfigBundle\Entity\User',
    'foo' => 'bar'
  ));
 }

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

As you can see in setDefaultOptions method you must declare the custom option. Then in your controller you can instantiate your form this way:

$myForm = $this->createForm(new UserType(), $entity, array('foo' => 'baz'));

Hope this helps

Upvotes: 1

Related Questions