Chapter 10 - Forms ================== Dealing with the display of form inputs, the validation of a form submission, and all the particular cases of forms is one of the most complex tasks in web development. Luckily, symfony provides a simple interface to a very powerful form sub-framework, and helps you to design and handle forms of any level of complexity in just a few lines of code. >**NOTICE**: This document is the first draft of a methodology experiment explained [earlier in this blog](http://redotheweb.com/2008/09/23/document-driven-development-in-practice-rethinking-sfforms/). It documents the sfForm framework found in symfony 1.1, but with some changes in the API and usage. As such, it describes a library that is not yet written (like that) and cannot be used to learn the usage of the current sfForm implementation. It is quite long, so you might prefer to [download the Markdown version]() and read it offline. Being a first draft, this document is a call for comments, both about its structure and its content. And if you are interested in implementing the differences between what this document describes and what is currently implemented in the symfony framework, please contact me. Displaying a Form ----------------- A simple contact form featuring a name, an email, a subject and a message fields typically renders as follows: ![Contact form](http://www.symfony-project.org/images/forms_book/en/01_07.png) In symfony, a form is an object defined in the action and passed to the template. In order to display a form, you must first define the fields it contains - symfony uses the term "widget". The simplest way to do it is to create a new `sfForm` object in the action method. [php] // in modules/foo/actions/actions.class.php public function executeContact($request) { $this->form = new sfForm(); $this->form->setWidgets(array( 'name' => 'text', 'email' => array('type' => 'text', 'default' => 'me@example.com'), 'subject' => array('type' => 'choice', 'choices' => array('Subject A', 'Subject B', 'Subject C')), 'message' => 'textarea' )); } `sfForm::setWidgets()` expects an associative array of widget name / widget definition. The definition must at least contain the widget type. `text`, `choice`, and `textarea` are some one the numerous widget types offered by symfony; you will find a complete list further in this chapter. For simple widgets, specifying the widget type name is enough. If you need to define more than just the widget type, use an associative array with at least a `type` key. The previous example shows two widget options you can use: `default` sets the widget value, and is available for all widgets. `choices` is an option specific to the `choice` widget (which renders as a drop-down list): it defines the available options the user can select. So the `foo/contact` action defines a form object, and then handles it to the `contactSuccess` template in a `$form` variable. The template can use this object to render the various parts of the form in HTML. The simplest way to do it is to call `echo $form`, and this will render all the fields as form controls with labels. You can also use the form object to generate the form tag: [php] // in modules/foo/templates/contactSuccess.php renderFormTag('foo/contact') ?>
With the parameters passed to `setWidgets()`, symfony has enough information to display the form correctly. The resulting HTML renders exactly like the screenshot above, with this underlying code: [php]
Each widget results in a table row containing a `