Login to bookmark this video
Buy Access to Course
12.

Beautiful Form Validation

Share this awesome video!

|

Keep on Learning!

Guess what! Server-side validation is really, really fun. Google for Symfony validation, and find the book chapter.

There is one weird thing about validation... which I love. Here it is: you don't apply validation to your form. Nope, there will be no validation code inside of GenusFormType. Instead, you add validation to the class that is bound to your form. When the form is submitted, it automatically reads those validation rules and uses them.

Validation is added with annotations. So copy the use statement from the code block, find Genus and paste it on top:

141 lines | src/AppBundle/Entity/Genus.php
// ... lines 1 - 6
use Symfony\Component\Validator\Constraints as Assert;
// ... lines 8 - 141

The Giant List of Constraints

Good start! Next, we'll add validation rules - called constraints - above each property. On the left side bar, find the "Constraints" link.

Check out this menu of validation rules: NotBlank, NotNull, Email, Length, Regex... so many things! Pretty much anything you can dream up is inside of this list.

Let's start with an easy one: above the name property, add @Assert\NotBlank:

141 lines | src/AppBundle/Entity/Genus.php
// ... lines 1 - 12
class Genus
{
// ... lines 15 - 21
/**
* @Assert\NotBlank()
// ... line 24
*/
private $name;
// ... lines 27 - 139
}

Without doing anything else, refresh. Boom! Validation error. And, it looks nice.

Let's add some more. For subFamily - that should be required, so add @NotBlank:

145 lines | src/AppBundle/Entity/Genus.php
// ... lines 1 - 12
class Genus
{
// ... lines 15 - 27
/**
* @Assert\NotBlank()
// ... lines 30 - 31
*/
private $subFamily;
// ... lines 34 - 143
}

For speciesCount, add @NotBlank again:

145 lines | src/AppBundle/Entity/Genus.php
// ... lines 1 - 12
class Genus
{
// ... lines 15 - 34
/**
* @Assert\NotBlank()
// ... lines 37 - 38
*/
private $speciesCount;
// ... lines 41 - 143
}

But in addition to that, we want speciesCount to be a positive number: we don't want some funny biologist entering negative 10.

Constraint Options

On the constraints list, there's one called Range. Check that out.

Ok cool: just like the form field types, you can pass options to the constraints. The Range constraint has several: min, max, minMessage and maxMessage. Add @Assert\Range with min=0 and minMessage="Negative species! Come on...":

145 lines | src/AppBundle/Entity/Genus.php
// ... lines 1 - 12
class Genus
{
// ... lines 15 - 34
/**
* @Assert\NotBlank()
* @Assert\Range(min=0, minMessage="Negative species! Come on...")
// ... line 38
*/
private $speciesCount;
// ... lines 41 - 143
}

Ok, let's finish up. It's ok if funFact is empty - so don't add anything there. The same is true for isPublished: we could add a constraint to make sure this is a boolean, but the sanity validation on the form already takes care of that.

Finally, let's make sure firstDiscoveredAt is also NotBlank:

145 lines | src/AppBundle/Entity/Genus.php
// ... lines 1 - 12
class Genus
{
// ... lines 15 - 51
/**
* @Assert\NotBlank()
// ... line 54
*/
private $firstDiscoveredAt;
// ... lines 57 - 143
}

Ok, refresh! Leave everything blank and put -10 for the number of species. I love it!