Book Image

Symfony2 Essentials

Book Image

Symfony2 Essentials

Overview of this book

Table of Contents (17 chapters)
Symfony2 Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Validation and form processing


So far, we have discussed the process of form creation. After we create the form, we usually need to validate the data provided, and we need to save the date somewhere. With Symfony2, these steps are pretty easy.

To make validation possible, we need to define rules for it. In Symfony2, we can define them in several ways. You can define constraints within the entity field (using annotations), within the form (as constraints option), and you can create validation YML configuration files. You should choose the way best for your own project clarity.

The easiest method is to provide validation within the form type, but again, it's not the best place to put constraints in (although it's useful if you are not using entities). If we work with entities, the best practice seems to be keeping the validation rules in the same place where we define the entity field.

Let's modify our entity class to support validation and make the name field required. Let's modify our entity...