0

As I said in my title, imagine I have one entity User and I want to create multiple form for this entity.

For example, this is my class User :

class User
{
    protected $firstname;
    protected $lastname;
    protected $sex;
    protected $birthdate
    protected $phone;
    protected $email;
    protected $password;
}

Now I want to create multiple form for this entity :

  • RegisterForm (ask every fields)
  • ConnexionForm (ask email + password)
  • ForgetPasswordForm (ask email)
  • etc.

I find this solution and I'd like to know if it's the best practice or not :

In my "FormType", I configure my options like this :

/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\User',
        'fields' => false,
    ));
}

Then I create a loop for each fields I want :

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    if($options['fields']){
       foreach($options['fields'] as $field){
                   $builder->add($field);
       }
    } else {
          // Every field I want
    }


}

And in my Controller, I can use it like this :

$form  = $this->createForm(UserType::class,$user,['fields' => ['email','password', 'other...']);

With the same idea, I maybe can give a name to my different form and if I don't write a name in my "options" I create the "Register" form, if I write "connexion" I build the "Connexion" form, etc.

Do you have a better solution or it's the best way to do it?

(I know you can do it with FOSUserBundle, but I pick "User" for the example)

1 Answer 1

1

You can do it like you wrote.

But i think better solution is to write specialized php form classes. Than you would have types like you wrote. But form classes are called with Type word in Symfony:

  • RegisterType (ask every fields)

  • ConnectionType (ask email + password)

  • ForgetPasswordType (ask email)

Here you have Best Practices and more tips from Symfony (first paragraph is about forms): http://symfony.com/doc/2.8/best_practices/forms.html

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks to anwser. So the best solution is to repeat almost the same code 3 times (in my example) and just change the fields I need? This is what I wanted to do first, but in my mind if you repeat yourself you don't optimize your code :s But if the Symfony best-practices are ok with that I think I will just follow the advices ^^
If you really won't change much in this classes and you feel it really breaks DRY you could use the options resolver as you point in your post. Sometimes i use it too. If you do so you should remember to put buttons in the view where you use your form. As they say in this paragraph: symfony.com/doc/2.8/best_practices/…
Since each form is very different in function, I think multiple form classes is the best solution. I know you're trying to not repeat your code, but at some point that gets taken to the extreme and you end up doing a bunch of tricks that may work but make your code a lot less clear. If I were looking to modify a registration form, I'd be looking for a RegisterType class, not a generic UserType class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.