Symfony2 validation
Post on 15-Jan-2015
4211 Views
Preview:
DESCRIPTION
Transcript
Production agency specializing in WebDevelopment www.void.fr
PHP framework www.symfony.com
http://twitter.com/baazzi
http://www.facebook.com/jBinfo
http://plus.google.com/113667438028898816639
Lhassan BaazziWeb Developper #php #Symfony2 at VOID
$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 ?
Summary$0
Don't TrustUser Input
Why ?$1
1. Validation is a very common task in webapplications.
2. Data entered in forms needs to bevalidated.
3. Data also needs to be validated before itis written into a database or passed to aweb service.
Why ?$1
The goal of validation isto tell you whether or not
the data of an object isvalid.
Goal$2
configure a list of rules (called constraints)that the object must follow in order to be
valid.
How ?$3
These constraints can be specified via anumber of different formats (YAML, XML,
annotations, or PHP).
a constraint is simply a PHP object thatmakes an assertive statement.
Constraint$4
Basic validation example$5For example, to guarantee that the $name property is notempty:
Basic validation example$5
Imports constraintsnamespace
Add NotBlankconstraint
For example, to guarantee that the $name property is notempty:
Basic validation example$5
The Symfony2 validator is enabled by default, but you mustexplicitly enable annotations if you're using the annotationmethod to specify your constraints:
Basic Constraints NotBlank Blank NotNull Null True False Type
String Constraints Email MinLength MaxLength Url Regex Ip
Date Constraints Date DateTime Time
Collection Constraints Choice Collection UniqueEntity Language Locale Country
Number Constraints Max Min
File Constraints File Image
Other Constraints Callback All Valid
Supported constraints$6
Basic Constraints NotBlank Blank NotNull Null True False Type
String Constraints Email MinLength MaxLength Url Regex Ip
Date Constraints Date DateTime Time
Collection Constraints Choice Collection UniqueEntity Language Locale Country
Number Constraints Max Min
File Constraints File Image
Other Constraints Callback All Valid
Supported constraints$6
The validator service$7
To validate an object, use thevalidate method on the validator service.
The validator service$7
Is to read the constraints (i.e. rules) of aclass and verify whether or not the dataon the object satisfies those constraints.
The job of the validator:
If validation fails, an array of errors isreturned.
The validator service$7
The validator service$7
Each validation error (called aconstraint violation), is represented
by a ConstraintViolation object.
ConstraintViolation: http://api.symfony.com/2.0/Symfony/Component/Validator/ConstraintViolation.html
Validation and Forms$8
Symfony's form library uses thevalidator service internally to
validate the underlying object aftervalues have been submitted and
bound.
Validation and Forms$8
The constraint violations on theobject are converted into FieldErrorobjects that can easily be displayed
with your form.
Validation and Forms$8
Translating constraint messages$9
Create a translation file under thevalidators catalog for the constraint
messages, typically in theResources/translations/ directory of
the bundle.
Translating constraint messages$9
Constraint message
Constraint message
Translation message
Constraint targets$10
Constraints can be applied toa class property (e.g. name)or a public getter method
(e.g. getFullName)
Constraint targets$10Properties:
The validator service allows you to validate private, protected or publicproperties.
The example below shows you how to configure the $firstName propertyof an Author class to have at least 3 characters:
Constraint targets$10
Getters:
Constraints can also be applied to the return value of amethod.
Validator service allows you to add a constraint to any publicmethod whose name starts with “get” or “is”. In this guide,both of these types of methods are referred to as “getters”.
Constraint targets$10Getters:
Constraint targets$10
Some constraints apply to the entire class beingvalidated.
For example, the Callback constraint is a genericconstraint that's applied to the class itself. When thatclass is validated, methods specified by that constraint
are simply executed so that each can provide morecustom validation.
Validation groups$11
How to validate an object againstonly some of the constraints on
that class ?
Question:
Validation groups$11
Organize each constraint into oneor more “validation groups”, andthen apply validation against justone or more group of constraints.
Answer:
Validation groups$11
Suppose you have a User class,which is used both when a user
registers and when a user updateshis/her contact information later:
Example:
Validation groups$11
Validation groups$11
With this configuration, there are two validationgroups: default: contains the constraints not
assigned to any other group; registration: contains the constraints on the
email and password fields only.
Validation groups$11
To tell the validator to use a specific group, passone or more group names as the secondargument to the validate() method:
Validation groups$11
validation groups in forms:
Controller:
Form Class:
Validating values$12
you've seen how you can validateentire objects. But sometimes, you
just want to validate a simple value -like to verify that a string is a valid
email address.
verify that a string is a valid email address:
Validating values$12
Import constraint Email
Create the consraint
Assigned the error message
Execute
Check for errors
How to create a custom validationconstraint ?$13
http://symfony.com/doc/current/cookbook/validation/custom_constraint.html
Questions ?
top related