10.04.2018 Views

Doctrine_manual-1-2-en

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10: Compon<strong>en</strong>t Overview 184<br />

example 2 as well as "7" are both valid integers whilst "3f" is not. Type validations<br />

occur on every column (since every column definition needs a type).<br />

• L<strong>en</strong>gth validation: As the name implies, all values assigned to columns are validated<br />

to make sure that the value does not exceed the maximum l<strong>en</strong>gth.<br />

You can combine the following constants by using bitwise operations: VALIDATE_ALL,<br />

VALIDATE_TYPES, VALIDATE_LENGTHS, VALIDATE_CONSTRAINTS, VALIDATE_NONE.<br />

For example to <strong>en</strong>able all validations except l<strong>en</strong>gth validations you would use:<br />

Listing<br />

10-95<br />

// bootstrap.php<br />

// ...<br />

$manager->setAttribute(<strong>Doctrine</strong>_Core::ATTR_VALIDATE, VALIDATE_ALL &<br />

~VALIDATE_LENGTHS);<br />

You can read more about this topic in the Data Validation (page 209) chapter.<br />

More Validation<br />

The type and l<strong>en</strong>gth validations are handy but most of the time they're not <strong>en</strong>ough. Therefore<br />

<strong>Doctrine</strong> provides some mechanisms that can be used to validate your data in more detail.<br />

Validators are an easy way to specify further validations. <strong>Doctrine</strong> has a lot of predefined<br />

validators that are frequ<strong>en</strong>tly needed such as email, country, ip, range and regexp<br />

validators. You find a full list of available validators in the Data Validation (page 209) chapter.<br />

You can specify which validators apply to which column through the 4th argum<strong>en</strong>t of the<br />

hasColumn() method. If that is still not <strong>en</strong>ough and you need some specialized validation<br />

that is not yet available as a predefined validator you have three options:<br />

• You can write the validator on your own.<br />

• You can propose your need for a new validator to a <strong>Doctrine</strong> developer.<br />

• You can use validation hooks.<br />

The first two options are advisable if it is likely that the validation is of g<strong>en</strong>eral use and is<br />

pot<strong>en</strong>tially applicable in many situations. In that case it is a good idea to implem<strong>en</strong>t a new<br />

validator. However if the validation is special it is better to use hooks provided by <strong>Doctrine</strong>:<br />

• validate() (Executed every time the record gets validated)<br />

• validateOnInsert() (Executed wh<strong>en</strong> the record is new and gets validated)<br />

• validateOnUpdate() (Executed wh<strong>en</strong> the record is not new and gets validated)<br />

If you need a special validation in your active record you can simply override one of these<br />

methods in your active record class (a desc<strong>en</strong>dant of <strong>Doctrine</strong>_Record). Within these<br />

methods you can use all the power of PHP to validate your fields. Wh<strong>en</strong> a field does not pass<br />

your validation you can th<strong>en</strong> add errors to the record's error stack. The following code<br />

snippet shows an example of how to define validators together with custom validation:<br />

Listing<br />

10-96<br />

// models/User.php<br />

class User ext<strong>en</strong>ds BaseUser<br />

{<br />

protected function validate()<br />

{<br />

if ($this->username == 'God') {<br />

// Blasphemy! Stop that! ;-)<br />

// syntax: add(, )<br />

$errorStack = $this->getErrorStack();<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!