Development

Documentation/fr_FR/book/1.0/10-Forms (diff)

You must first sign up to be able to contribute.

Changes between Version 4 and Version 5 of Documentation/fr_FR/book/1.0/10-Forms

Show
Ignore:
Author:
Saad.Tazi (IP: 74.56.92.194)
Timestamp:
06/06/07 03:21:34 (11 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Documentation/fr_FR/book/1.0/10-Forms

    v4 v5  
    1414In templates, HTML tags of form elements are very often mixed with PHP code. Form helpers in symfony aim to simplify this task and to avoid opening `<?php echo` tags repeatedly in the middle of `<input>` tags. 
    1515 
    16 ### Main Form Tag 
     16=== Main Form Tag === 
    1717 
    1818As explained in the previous chapter, you must use the `form_tag()` helper to create a form, since it transforms the action given as a parameter into a routed URL. The second argument can support additional options--for instance, to change the default `method`, change the default `enctype`, or specify other attributes. Listing 10-1 shows examples. 
    2929As there is no need for a closing form helper, you should use the HTML `</form>` tag, even if it doesn't look good in your source code. 
    3030 
    31 ### Standard Form Elements 
     31=== Standard Form Elements === 
    3232 
    3333With form helpers, each element in a form is given an id attribute deduced from its name attribute by default. This is not the only useful convention. See Listing 10-2 for a full list of standard form helpers and their options. 
    193193Symfony offers specialized form helpers to do asynchronous requests in the background. The next chapter, which focuses on Ajax, provides more details. 
    194194 
    195 ### Date Input Widgets 
     195=== Date Input Widgets === 
    196196 
    197197Forms are often used to retrieve dates. Dates in the wrong format are the main reason for form-submission failures. The `input_date_tag()` helper can assist the user in entering a date with an interactive JavaScript calendar, if you set the `rich` option to `true`, as shown in Figure 10-1. 
    263263    <?php echo input_date_tag('test', '01/04/2006', 'rich=true') ?> 
    264264 
    265 ### Rich Text Editing 
     265=== Rich Text Editing === 
    266266 
    267267Rich text editing is also possible in a `<textarea>` tag, thanks to the integration of the TinyMCE and FCKEditor widgets. They provide a word-processor-like interface with buttons to format text as bold, italic, and other styles, as shown in Figure 10-2. 
    289289    => a rich text edit zone powered by TinyMCE with custom parameters 
    290290 
    291 ### Country and Language Selection 
     291=== Country and Language Selection === 
    292292 
    293293You may need to display a country selection field. But since country names are not the same in all languages, the options of a country drop-down list should vary according to the user culture (see Chapter 13 for more information about cultures). As shown in Listing 10-8, the `select_country_tag()` helper does it all for you: It internationalizes country names and uses the standard ISO country codes for values. 
    320320          ... 
    321321 
    322 Form Helpers for Objects 
    323 ------------------------ 
     322== Form Helpers for Objects == 
     323 
    324324 
    325325When form elements are used to edit the properties of an object, standard link helpers can become tedious to write. For instance, to edit the `telephone` attribute of a `Customer` object, you would write this: 
    356356The most interesting of all object form helpers are `objects_for_select()` and `object_select_tag()`, which concern drop-down lists. 
    357357 
    358 ### Populating Drop-Down Lists with Objects 
     358=== Populating Drop-Down Lists with Objects === 
    359359 
    360360The `options_for_select()` helper, described previously with the other standard helpers, transforms a PHP associative array into an options list, as shown in Listing 10-11. 
    449449      'include_custom=Choose an author') ?> 
    450450 
    451 ### Updating Objects 
     451=== Updating Objects === 
    452452 
    453453A form completely dedicated to editing object properties by using object helpers is easier to handle in an action. For instance, if you have an object of class `Author` with `name`, `age`, and `address` attributes, the form can be coded as shown in Listing 10-15. 
    480480    } 
    481481 
    482 Form Validation 
    483 --------------- 
     482== Form Validation == 
     483 
    484484 
    485485Chapter 6 explained how to use the `validateXXX()` methods in the action class to validate the request parameters. However, if you use this technique to validate a form submission, you will end up rewriting the same portion of code over and over. Symfony provides an alternative form-validation technique, relying on only a YAML file, instead of PHP code in the action class. 
    510510>Form validation can occur on the server side and/or on the client side. The server-side validation is compulsory to avoid corrupting a database with wrong data. The client-side validation is optional, though it greatly enhances the user experience. The client-side validation is to be done with custom JavaScript. 
    511511 
    512 ### Validators 
     512=== Validators === 
    513513 
    514514You can see that the `name` and `email` fields in the example share common validation rules. Some validation rules appear so often in web forms that symfony packages the PHP code that implements them into validators. A validator is simple class that provides an `execute()` method. This method expects the value of a field as parameter, and returns `true` if the value is valid and `false` otherwise. 
    560560The fact that a field is required is not handled by a validator. 
    561561 
    562 ### Validation File 
     562=== Validation File === 
    563563 
    564564You could easily implement the validation of the contact form with validators in the `validateSend()` method PHP, but that would imply repeating a lot of code. Symfony offers an alternative way to define validation rules for a form, and it involves YAML. For instance, Listing 10-19 shows the translation of the name field validation rules, and its results are equivalent to those of Listing 10-18. 
    583583Validation files are located in the module `validate/` directory, and named by the action they must validate. For example, Listing 10-19 must be stored in a file called `validate/send.yml`. 
    584584 
    585 ### Redisplaying the Form 
     585=== Redisplaying the Form === 
    586586 
    587587By default, symfony looks for a `handleErrorSend()` method in the action class whenever the validation process fails, or displays the `sendError.php` template if the method doesn't exist. 
    646646With this new configuration, when the user types an invalid name, the form is displayed again, but the entered data is lost and no error message explains the reason of the failure. To address the last issue, you must modify the template that displays the form, to insert error messages close to the faulty field. 
    647647 
    648 ### Displaying the Error Messages in the Form 
     648=== Displaying the Error Messages in the Form === 
    649649 
    650650The error messages defined as validator parameters are added to the request when a field fails validation (just as you can add an error manually with the `setError()` method, as in Listing 10-18). The `sfRequest` object provides two useful methods to retrieve the error message: `hasError()` and `getError()`, which each expect a field name as parameter. In addition, you can display an alert at the top of the form to draw attention to the fact that one or many of the fields contain invalid data with the `hasErrors()` method. Listings 10-22 and 10-23 demonstrate how to use these methods. 
    698698In case of failed validation, the form now displays errors correctly, but the data entered by the user is lost. You need to repopulate the form to make it really user-friendly. 
    699699 
    700 ### Repopulating the Form 
     700=== Repopulating the Form === 
    701701 
    702702As the error handling is done through the `forward()` method (shown in Listing 10-20), the original request is still accessible, and the data entered by the user is in the request parameters. So you could repopulate the form by adding default values to each field, as shown in Listing 10-25. 
    747747          htmlspecialchars: [comments] 
    748748 
    749 ### Standard Symfony Validators 
     749=== Standard Symfony Validators === 
    750750 
    751751Symfony contains some standard validators that can be used for your forms: 
    763763Each has a default set of parameters and error messages, but you can easily override them through the `initialize()` validator method or in the YAML file. The following sections describe the validators and show usage examples. 
    764764 
    765 #### String Validator 
     765==== String Validator ==== 
    766766 
    767767`sfStringValidator` allows you to apply string-related constraints to a parameter. 
    776776      max_error:    Please enter less than 100 characters 
    777777 
    778 #### Number Validator 
     778==== Number Validator ==== 
    779779 
    780780`sfNumberValidator` verifies if a parameter is a number and allows you to apply size constraints. 
    787787      max_error:    The value must be less than 100 
    788788 
    789 #### E-Mail Validator 
     789==== E-Mail Validator ==== 
    790790 
    791791`sfEmailValidator` verifies if a parameter contains a value that qualifies as an e-mail address. 
    797797RFC822 defines the format of e-mail addresses. However, it is more permissive than the generally accepted format. For instance, `me@localhost` is a valid e-mail address according to the RFC, but you probably don't want to accept it. When the `strict` parameter is set to `true` (its default value), only e-mail addresses matching the pattern `name@domain.extension` are valid. When set to `false`, RFC822 is used as a rule. 
    798798 
    799 #### URL Validator 
     799==== URL Validator ==== 
    800800 
    801801`sfUrlValidator` checks if a field is a correct URL. 
    804804      url_error:    This URL is invalid 
    805805 
    806 #### Regular Expression Validator 
     806==== Regular Expression Validator ==== 
    807807 
    808808`sfRegexValidator` allows you to match a value against a Perl-compatible regular expression pattern. 
    815815The `match` parameter determines if the request parameter must match the pattern to be valid (value `Yes`) or match the pattern to be invalid (value `No`). 
    816816 
    817 #### Compare Validator 
     817==== Compare Validator ==== 
    818818 
    819819`sfCompareValidator` compares two different request parameters. It is very useful for password checks. 
    834834By default, the validator checks the equality of the parameters. You can change this behavior be specifying an `operator` parameter. Available operators are: `>`, `>=`, `<`, `<=`, `==` and `!=`. 
    835835 
    836 #### Propel Unique Validator 
     836==== Propel Unique Validator ==== 
    837837 
    838838`sfPropelUniqueValidator` validates that the value of a request parameter doesn't already exist in your database. It is very useful for unique indexes. 
    847847In this example, the validator will look in the database for a record of class `User` where the `login` column has the same value as the field to validate. 
    848848 
    849 #### File Validator 
     849==== File Validator ==== 
    850850 
    851851`sfFileValidator` applies format (an array of mime-types) and size constraints to file upload fields. 
    868868Be aware that the `file` attribute must be set to `True` for the field, and the template must declare the form as multipart. 
    869869 
    870 #### Callback Validator 
     870==== Callback Validator ==== 
    871871 
    872872`sfCallbackValidator` delegates the validation to a third-party callable method or function to do the validation. The callable method or function must return `true` or `false`. 
    887887>You can also write your own validators, as described in the "Creating a Custom Validator" section later in this chapter. 
    888888 
    889 ### Named Validators 
     889### Named Validators === 
    890890 
    891891If you see that you need to repeat a validator class and its settings, you can package it under a named validator. In the example of the contact form, the email field needs the same sfStringValidator parameters as the `name` field. So you can create a `myStringValidator` named validator to avoid repeating the same settings twice. To do so, add a `myStringValidator` label under the `validators:` header, and set the `class` and `param` keys with the details of the named validator you want to package. You can then use the named validator just like a regular one in the `fields` section, as shown in Listing 10-28.