0

Actually I'm rather confused about the best way to pass arguments to a form. As far as I know, there are three ways:

  1. To turn the form class into a service

  2. To pass arguments through the array options

  3. To pass argument through the constructor of the form class

What would be the best/cleanest way?

2 Answers 2

1

In my opinion:

  1. To turn the form class into a service - you use if arguments are available by DI, like EntityManager, Router and other services etc.
  2. To pass arguments through the array options - "static" data like: show this field for admin only,
Sign up to request clarification or add additional context in comments.

4 Comments

So you're saying that if we need to logger for example it's only nicer to turn the form into a service. But what I think is when the form is called from a controller, it's way easier to just pass args through either the constructor or the array $options: you don't have to declare your form into your services file, you don't have to update the services file every time you realize you need another dependency etc...
It's just my opinion. I prefer clean code even if i have to modify service declaration. Less code in controller = less errors. Next improvement is i don't have to modify controller to change form in example: add some field. I only have to override service which is much simpler to do and don't duplicate code (controller) to do this. FormType as service in my case always are big and complicated so for me it's better solution.
I agree with you, as a service the form is much more standalone. +1
@smarber - One problem with injecting dependencies manually in the controller is that you have to remember to do so and to get them in the correct order. If another dependency needs to be added then you to find every controller that uses that form and update your code. Finally, if you to use the form as part of a collection then you are really stuck.
0

For the majority of my forms, I create a separate form class. When I need to render the form with prepopulated data, I load the entity in my controller and pass that entity when construction the form. For example, if I have an entity called $user linked to registration entity, I will populate my form like this:

    $registration = $user->getRegistration();

    $form = $this->createForm(new RegistrationType(), $registration, array());

Then I render the $form in twig:

    return $this->render(
       'AcmeAccountBundle:Account:register.html.twig',
        array('form' => $form->createView())
    );

2 Comments

And how does this answer the question of passing arguments to the form?
the $registration variable is the same type that the registration form uses and this will populate the form. When you said pass arguments, I assume you meant you want the fields to be populated with specific items and this will do that.

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.