Production agency specializing in Web                                        PHP framework www.symfony.comDevelopment www....
Lhassan Baazzi      Web Developper #php #Symfony2 at VOIDhttp://twitter.com/baazzihttp://www.facebook.com/jBinfohttp://plu...
$0 Summary$1- Why ?$2- Goal$3- How ?$4- What is a constraint ?$5- Basic validation example$6- Supported constraints$7- The...
$1 Why ?    Dont Trust    User Input
$1 Why ?1. Validation is a very common task in web   applications.2. Data entered in forms needs to be   validated.3. Data...
$2 Goal    The goal of validation is   to tell you whether or not     the data of an object is              valid.
$3 How ? configure a list of rules (called constraints)  that the object must follow in order to be                      v...
$4 Constraint  a constraint is simply a PHP object that      makes an assertive statement.
$5 Basic validation exampleFor example, to guarantee that the $name property is notempty:
$5 Basic validation exampleFor example, to guarantee that the $name property is notempty:                                 ...
$5 Basic validation example The Symfony2 validator is enabled by default, but you must explicitly enable annotations if yo...
$6 Supported constraintsBasic Constraints      String Constraints           Collection Constraints NotBlank             ...
$6 Supported constraintsBasic Constraints      String Constraints           Collection Constraints NotBlank             ...
$7 The validator service         To validate an object, use the   validate method on the validator service.
$7 The validator serviceThe job of the validator:     Is to read the constraints (i.e. rules) of a     class and verify wh...
$7 The validator service
$7 The validator service     Each validation error (called a  constraint violation), is represented    by a ConstraintViol...
$8 Validation and Forms     Symfonys form library uses the      validator service internally to   validate the underlying ...
$8 Validation and Forms    The constraint violations on the   object are converted into FieldError   objects that can easi...
$8 Validation and Forms
$9 Translating constraint messages    Create a translation file under the   validators catalog for the constraint        m...
$9 Translating constraint messages                    Constraint message                            Constraint message    ...
$10 Constraint targets   Constraints can be applied to   a class property (e.g. name)    or a public getter method        ...
$10 Constraint targetsProperties:  The validator service allows you to validate private, protected or public  properties. ...
$10 Constraint targetsGetters: Constraints can also be applied to the return value of a method. Validator service allows y...
$10 Constraint targetsGetters:
$10 Constraint targets    Some constraints apply to the entire class being                      validated.    For example,...
$11 Validation groupsQuestion:   How to validate an object against    only some of the constraints on              that cl...
$11 Validation groupsAnswer:   Organize each constraint into one   or more “validation groups”, and   then apply validatio...
$11 Validation groupsExample:    Suppose you have a User class,    which is used both when a user  registers and when a us...
$11 Validation groups
$11 Validation groups  With this configuration, there are two validation  groups:   default: contains the constraints not...
$11 Validation groups  To tell the validator to use a specific group, pass  one or more group names as the second  argumen...
$11 Validation groupsvalidation groups in forms:      Controller:      Form Class:
$12 Validating values     youve seen how you can validate    entire objects. But sometimes, you   just want to validate a ...
$12 Validating valuesverify that a string is a valid email address:                                 Import constraint Emai...
How to create a custom validation$13   constraint ?http://symfony.com/doc/current/cookbook/     validation/custom_constrai...
Questions ?
Symfony2 validation
Upcoming SlideShare
Loading in …5
×

Symfony2 validation

5,076 views
4,915 views

Published on

How to use the validator component in Symfony2 to validate datas ?

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,076
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
16
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Symfony2 validation

  1. 1. Production agency specializing in Web PHP framework www.symfony.comDevelopment www.void.fr
  2. 2. Lhassan Baazzi Web Developper #php #Symfony2 at VOIDhttp://twitter.com/baazzihttp://www.facebook.com/jBinfohttp://plus.google.com/113667438028898816639
  3. 3. $0 Summary$1- Why ?$2- Goal$3- How ?$4- What is a constraint ?$5- Basic validation example$6- Supported constraints$7- The validator service$8- Validation and Forms$9- Translation constraint messages$10- Constraint targets$11- Validation groups$12- Validating values$13- How to create a custom validation constraint ?
  4. 4. $1 Why ? Dont Trust User Input
  5. 5. $1 Why ?1. Validation is a very common task in web applications.2. Data entered in forms needs to be validated.3. Data also needs to be validated before it is written into a database or passed to a web service.
  6. 6. $2 Goal The goal of validation is to tell you whether or not the data of an object is valid.
  7. 7. $3 How ? configure a list of rules (called constraints) that the object must follow in order to be valid. These constraints can be specified via a number of different formats (YAML, XML, annotations, or PHP).
  8. 8. $4 Constraint a constraint is simply a PHP object that makes an assertive statement.
  9. 9. $5 Basic validation exampleFor example, to guarantee that the $name property is notempty:
  10. 10. $5 Basic validation exampleFor example, to guarantee that the $name property is notempty: Imports constraints namespace Add NotBlank constraint
  11. 11. $5 Basic validation example The Symfony2 validator is enabled by default, but you must explicitly enable annotations if youre using the annotation method to specify your constraints:
  12. 12. $6 Supported constraintsBasic Constraints String Constraints Collection Constraints NotBlank  Email  Choice Blank  MinLength  Collection NotNull  MaxLength  UniqueEntity Null  Url  Language True  Regex  Locale False  Ip  Country TypeNumber Constraints Date Constraints File Constraints Other Constraints Max  Date  File  Callback Min  DateTime  Image  All  Time  Valid
  13. 13. $6 Supported constraintsBasic Constraints String Constraints Collection Constraints NotBlank  Email  Choice Blank  MinLength  Collection NotNull  MaxLength  UniqueEntity Null  Url  Language True  Regex  Locale False  Ip  Country TypeNumber Constraints Date Constraints File Constraints Other Constraints Max  Date  File  Callback Min  DateTime  Image  All  Time  Valid
  14. 14. $7 The validator service To validate an object, use the validate method on the validator service.
  15. 15. $7 The validator serviceThe job of the validator: Is to read the constraints (i.e. rules) of a class and verify whether or not the data on the object satisfies those constraints. If validation fails, an array of errors is returned.
  16. 16. $7 The validator service
  17. 17. $7 The validator service Each validation error (called a constraint violation), is represented by a ConstraintViolation object. ConstraintViolation: http://api.symfony.com/2.0/Symfony/Component/Validator/ConstraintViolation.html
  18. 18. $8 Validation and Forms Symfonys form library uses the validator service internally to validate the underlying object after values have been submitted and bound.
  19. 19. $8 Validation and Forms The constraint violations on the object are converted into FieldError objects that can easily be displayed with your form.
  20. 20. $8 Validation and Forms
  21. 21. $9 Translating constraint messages Create a translation file under the validators catalog for the constraint messages, typically in the Resources/translations/ directory of the bundle.
  22. 22. $9 Translating constraint messages Constraint message Constraint message Translation message
  23. 23. $10 Constraint targets Constraints can be applied to a class property (e.g. name) or a public getter method (e.g. getFullName)
  24. 24. $10 Constraint targetsProperties: The validator service allows you to validate private, protected or public properties. The example below shows you how to configure the $firstName property of an Author class to have at least 3 characters:
  25. 25. $10 Constraint targetsGetters: Constraints can also be applied to the return value of a method. Validator service allows you to add a constraint to any public method whose name starts with “get” or “is”. In this guide, both of these types of methods are referred to as “getters”.
  26. 26. $10 Constraint targetsGetters:
  27. 27. $10 Constraint targets Some constraints apply to the entire class being validated. For example, the Callback constraint is a generic constraint thats applied to the class itself. When that class is validated, methods specified by that constraint are simply executed so that each can provide more custom validation.
  28. 28. $11 Validation groupsQuestion: How to validate an object against only some of the constraints on that class ?
  29. 29. $11 Validation groupsAnswer: Organize each constraint into one or more “validation groups”, and then apply validation against just one or more group of constraints.
  30. 30. $11 Validation groupsExample: Suppose you have a User class, which is used both when a user registers and when a user updates his/her contact information later:
  31. 31. $11 Validation groups
  32. 32. $11 Validation groups With this configuration, there are two validation groups:  default: contains the constraints not assigned to any other group;  registration: contains the constraints on the email and password fields only.
  33. 33. $11 Validation groups To tell the validator to use a specific group, pass one or more group names as the second argument to the validate() method:
  34. 34. $11 Validation groupsvalidation groups in forms: Controller: Form Class:
  35. 35. $12 Validating values youve seen how you can validate entire objects. But sometimes, you just want to validate a simple value - like to verify that a string is a valid email address.
  36. 36. $12 Validating valuesverify that a string is a valid email address: Import constraint Email Create the consraint Assigned the error message Execute Check for errors
  37. 37. How to create a custom validation$13 constraint ?http://symfony.com/doc/current/cookbook/ validation/custom_constraint.html
  38. 38. Questions ?

×