Формы в symfony 1.1
Translations of this material:
- into Russian: Формы в symfony 1.1. 98% translated in draft. Almost done, let's finish it!
-
Submitted for translation by akorenskoy84 10.07.2008
Text
A form is made of fields like hidden inputs, text inputs, select boxes, and checkboxes. This chapter introduces you to creating forms and managing form fields using the symfony forms framework.
Symfony 1.1 is required to follow the chapters of this book. You will also need to create a project and a frontend application to keep going. Please refer to the introduction for more information on symfony project creation.
Before we start
We will begin by adding a contact form to a symfony application.
Figure 1-1 shows the contact form as seen by users who want to send a message.
Figure 1-1 - Contact form
Contact form
We will create three fields for this form: the name of the user, the email of the user, and the message the user wants to send. We will simply display the information submitted in the form for the purpose of this exercise as shown in Figure 1-2.
Figure 1-2 - Thank you Page
Thank you page
Figure 1-3 - Shows the interaction between the application and the user.
Figure 1-3 - Interaction with the User
Interaction with the user schema
Widgets
sfForm and sfWidget Classes
Users input information into fields which make up forms. In symfony, a form is an object inheriting from the sfForm class. In our example, we will create a ContactForm class inheriting from the sfForm class. sfForm is the base class of all forms. sfForm makes it easy to manage the configuration and the life cycle of your forms.
sfForm is the base class of all forms and makes it easy to manage the configuration and life cycle of your forms.
You can start configuring your form by adding widgets using the configure() method.
A widget represents a form field. For our form example, we need to add three widgets representing our three fields: name, email, and message. Listing 1-1 shows the first implementation of the ContactForm class.
Listing 1-1 - ContactForm class with three fields
// lib/form/ContactForm.class.php
class ContactForm extends sfForm
{
public function configure()
{
$this->setWidgets(array(
'name' => new sfWidgetFormInput(),
'email' => new sfWidgetFormInput(),
'message' => new sfWidgetFormTextarea(),
));
}
}
The widgets are defined in the configure() method. This method is automatically called by the sfForm class constructor.
The setWidgets() method is used to define the widgets used in the form. The setWidgets() method accepts an associative array where the keys are the field names and the values are the widget objects. Each widget is an object inheriting from the sfWidget class. For this example we used two types of widgets:
* sfWidgetFormInput : This widget represents the input field
* sfWidgetFormTextarea: This widget represents the textarea field
As a convention, we store the form classes in a lib/form/ directory. You can store them in any directory managed by the symfony autoloading mechanism but as we will see later, symfony uses the lib/form/ directory to generate forms from model objects.
Displaying the Form
Our form is now ready to be used. We can now create a symfony module to display the form:
$ cd ~/PATH/TO/THE/PROJECT
$ php symfony generate:module frontend contact
In the contact module, let's modify the index action to pass a form instance to the template as shown in Listing 1-2.
Listing 1-2 - Actions class from the contact Module
// apps/frontend/modules/contact/actions/actions.class.php
class contactActions extends sfActions
{
public function executeIndex()
{
$this->form = new ContactForm();
}
}
When creating a form, the configure() method, defined earlier, will be called automatically.
We just need to create a template now to display the form as shown in Listing 1-3.
Listing 1-3 - Template displaying the form
// apps/frontend/modules/contact/templates/indexSuccess.php
" method="POST">
A symfony form only handles widgets displaying information to users. In the indexSuccess template, the line only displays three fields. The other elements such as the form tag and the submit button will need to be added by the developer. This might not look obvious at first, but we will see later how useful and easy it is to imbricate forms.
Using the construction is very useful when creating prototypes and defining forms. It allows developers to concentrate on the business logic without worrying about visual aspects. Chapter three will explain how to personalize the template and form layout.
When displaying an object using the , the PHP engine will actually display the text representation of the $form object. To convert the object into a string, PHP tries to execute the magic method __toString(). Each widget implements this magic method to convert the object into HTML code. Calling is then equivalent to calling __toString() ?>.
We can now see the form in a browser (Figure 1-4) and check the result by typing the address of the action contact/index (/frontend_dev.php/contact).
Figure 1-4 - Generated Contact Form
Generated Contact Form
Listing 1-4 Shows the generated code by the template.
-->
Name
Message
-->
© sensiolabs. License: GFDL license
