Download - Symfony2 validation

Transcript
Page 1: Symfony2 validation

Production agency specializing in WebDevelopment www.void.fr

PHP framework www.symfony.com

Page 2: Symfony2 validation

http://twitter.com/baazzi

http://www.facebook.com/jBinfo

http://plus.google.com/113667438028898816639

Lhassan BaazziWeb Developper #php #Symfony2 at VOID

Page 3: Symfony2 validation
Page 4: Symfony2 validation

$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

Page 5: Symfony2 validation

Don't TrustUser Input

Why ?$1

Page 6: Symfony2 validation

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

Page 7: Symfony2 validation

The goal of validation isto tell you whether or not

the data of an object isvalid.

Goal$2

Page 8: Symfony2 validation

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).

Page 9: Symfony2 validation

a constraint is simply a PHP object thatmakes an assertive statement.

Constraint$4

Page 10: Symfony2 validation

Basic validation example$5For example, to guarantee that the $name property is notempty:

Page 11: Symfony2 validation

Basic validation example$5

Imports constraintsnamespace

Add NotBlankconstraint

For example, to guarantee that the $name property is notempty:

Page 12: Symfony2 validation

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:

Page 13: Symfony2 validation

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

Page 14: Symfony2 validation

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

Page 15: Symfony2 validation

The validator service$7

To validate an object, use thevalidate method on the validator service.

Page 16: Symfony2 validation

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.

Page 17: Symfony2 validation

The validator service$7

Page 18: Symfony2 validation

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

Page 19: Symfony2 validation

Validation and Forms$8

Symfony's form library uses thevalidator service internally to

validate the underlying object aftervalues have been submitted and

bound.

Page 20: Symfony2 validation

Validation and Forms$8

The constraint violations on theobject are converted into FieldErrorobjects that can easily be displayed

with your form.

Page 21: Symfony2 validation

Validation and Forms$8

Page 22: Symfony2 validation

Translating constraint messages$9

Create a translation file under thevalidators catalog for the constraint

messages, typically in theResources/translations/ directory of

the bundle.

Page 23: Symfony2 validation

Translating constraint messages$9

Constraint message

Constraint message

Translation message

Page 24: Symfony2 validation

Constraint targets$10

Constraints can be applied toa class property (e.g. name)or a public getter method

(e.g. getFullName)

Page 25: Symfony2 validation

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:

Page 26: Symfony2 validation

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”.

Page 27: Symfony2 validation

Constraint targets$10Getters:

Page 28: Symfony2 validation

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.

Page 29: Symfony2 validation

Validation groups$11

How to validate an object againstonly some of the constraints on

that class ?

Question:

Page 30: Symfony2 validation

Validation groups$11

Organize each constraint into oneor more “validation groups”, andthen apply validation against justone or more group of constraints.

Answer:

Page 31: Symfony2 validation

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:

Page 32: Symfony2 validation

Validation groups$11

Page 33: Symfony2 validation

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.

Page 34: Symfony2 validation

Validation groups$11

To tell the validator to use a specific group, passone or more group names as the secondargument to the validate() method:

Page 35: Symfony2 validation

Validation groups$11

validation groups in forms:

Controller:

Form Class:

Page 36: Symfony2 validation

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.

Page 37: Symfony2 validation

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

Page 38: Symfony2 validation

How to create a custom validationconstraint ?$13

http://symfony.com/doc/current/cookbook/validation/custom_constraint.html

Page 39: Symfony2 validation

Questions ?