Production agency specializing in Web
Development www.void.fr
PHP framework www.symfony.com
Slide 2
Slide 2 text
http://twitter.com/baazzi
http://www.facebook.com/jBinfo
http://plus.google.com/113667438028898816639
Lhassan Baazzi
Web Developper #php #Symfony2 at VOID
Slide 3
Slide 3 text
No content
Slide 4
Slide 4 text
$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
Slide 5
Slide 5 text
Don't Trust
User Input
Why ?
$1
Slide 6
Slide 6 text
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.
Why ?
$1
Slide 7
Slide 7 text
The goal of validation is
to tell you whether or not
the data of an object is
valid.
Goal
$2
Slide 8
Slide 8 text
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 a
number of different formats (YAML, XML,
annotations, or PHP).
Slide 9
Slide 9 text
a constraint is simply a PHP object that
makes an assertive statement.
Constraint
$4
Slide 10
Slide 10 text
Basic validation example
$5
For example, to guarantee that the $name property is not
empty:
Slide 11
Slide 11 text
Basic validation example
$5
Imports constraints
namespace
Add NotBlank
constraint
For example, to guarantee that the $name property is not
empty:
Slide 12
Slide 12 text
Basic validation example
$5
The Symfony2 validator is enabled by default, but you must
explicitly enable annotations if you're using the annotation
method to specify your constraints:
Slide 13
Slide 13 text
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
Slide 14
Slide 14 text
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
Slide 15
Slide 15 text
The validator service
$7
To validate an object, use the
validate method on the validator service.
Slide 16
Slide 16 text
The validator service
$7
Is to read the constraints (i.e. rules) of a
class and verify whether or not the data
on the object satisfies those constraints.
The job of the validator:
If validation fails, an array of errors is
returned.
Slide 17
Slide 17 text
The validator service
$7
Slide 18
Slide 18 text
The validator service
$7
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
Slide 19
Slide 19 text
Validation and Forms
$8
Symfony's form library uses the
validator service internally to
validate the underlying object after
values have been submitted and
bound.
Slide 20
Slide 20 text
Validation and Forms
$8
The constraint violations on the
object are converted into FieldError
objects that can easily be displayed
with your form.
Slide 21
Slide 21 text
Validation and Forms
$8
Slide 22
Slide 22 text
Translating constraint messages
$9
Create a translation file under the
validators catalog for the constraint
messages, typically in the
Resources/translations/ directory of
the bundle.
Constraint targets
$10
Constraints can be applied to
a class property (e.g. name)
or a public getter method
(e.g. getFullName)
Slide 25
Slide 25 text
Constraint targets
$10
Properties:
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:
Slide 26
Slide 26 text
Constraint targets
$10
Getters:
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”.
Slide 27
Slide 27 text
Constraint targets
$10
Getters:
Slide 28
Slide 28 text
Constraint targets
$10
Some constraints apply to the entire class being
validated.
For example, the Callback constraint is a generic
constraint that's 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.
Slide 29
Slide 29 text
Validation groups
$11
How to validate an object against
only some of the constraints on
that class ?
Question:
Slide 30
Slide 30 text
Validation groups
$11
Organize each constraint into one
or more “validation groups”, and
then apply validation against just
one or more group of constraints.
Answer:
Slide 31
Slide 31 text
Validation groups
$11
Suppose you have a User class,
which is used both when a user
registers and when a user updates
his/her contact information later:
Example:
Slide 32
Slide 32 text
Validation groups
$11
Slide 33
Slide 33 text
Validation groups
$11
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.
Slide 34
Slide 34 text
Validation groups
$11
To tell the validator to use a specific group, pass
one or more group names as the second
argument to the validate() method:
Slide 35
Slide 35 text
Validation groups
$11
validation groups in forms:
Controller:
Form Class:
Slide 36
Slide 36 text
Validating values
$12
you've 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.
Slide 37
Slide 37 text
verify that a string is a valid email address:
Validating values
$12
Import constraint Email
Import constraint Email
Create the consraint
Create the consraint
Assigned the error message
Assigned the error message
Execute
Execute
Check for errors
Check for errors
Slide 38
Slide 38 text
How to create a custom validation
constraint ?
$13
http://symfony.com/doc/current/cookbook/
validation/custom_constraint.html