0

As far as I know symfony 2 contains its own form builder. However I don't really know how to connect it with form classes...

To demonstrate what I mean let me write simple piece of code I used combined with standard HTML form:

public function proccessFormAction()
{
   if(! $_POST)
   {
      return $this->render('::ViewWithForm.html.twig');
   }
   else // after clicking submit in the form above
   {
      // validation, and other operations
   }
}

Creating separate form classes handling all of this would be great, but...and here comes the first question - where should I place these classes? Just throw them in into the bundle's controllers?

Also I've been thinking about displaying form from inside the processFormAction() method, and making all kind of operations on them in form class - is this a good idea?

By form class I mean this Task class from the tutorial.

4
  • What do you mean by 'form classes'? In most cases, you'll have an entity you'll want to bind to. To facilitate the data binding and form creation process, you'd create a form type class (like UserType.php) that would tell the form builder how to represent that entity (in this case, a User) as form fields. Is that what you mean? Commented May 24, 2013 at 22:14
  • Yup, that's exactly what I mean. Commented May 24, 2013 at 22:18
  • Okay, those form type classes would go into /src/path/to/bundle/Form/Type directory. So, the Form directory would be at the same level as Controller and Resources and would contain a Type subdirectory Commented May 24, 2013 at 22:22
  • The form types should be dumb. They should only care about properly displaying form fields so the entity can be represented by form fields. Any additional processing should likely be done in the controller. You could also do processing through the entities themselves, but that gets into domain-driven design territory, which may be outside the scope of your question. Commented May 24, 2013 at 22:26

1 Answer 1

2

The best entry point to get an idea of how creating and processing forms with symfony2 works ...

...is the Forms chapter of the Book.

It contains code examples and a pretty good quick introduction into whats going on under the hood.

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

3 Comments

In every controller in my bundle I have a couple of different forms...Can I divide this bundle/Form folder into subfolders matching controllers' names?
The general convention is to prefix the FormType names with the name of the corresponding entity i.e class UserFormType in the folder Vendor/YourBundle/Form/Type/UserFormType.php ... But of course you can give them whatever name and put them in any directory you want as long as your directory and file/class-naming structure and namespace follow the psr-0 convention.
Hm, wait a second...so the Form/Type folder's classes do same thing as Task class from here: symfony.com/doc/master/book/forms.html ?

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.