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 13: Data Validation 219<br />

# ...<br />

type: string(15)<br />

ip: true<br />

Now if you try and save a User with an invalid ip address the validation will fail.<br />

$user = new User();<br />

$user->username = 'jwage';<br />

$user->ip_address = '123.123';<br />

Listing<br />

13-28<br />

if ( ! $user->isValid()) {<br />

echo 'User is invalid because "123.123" is not a valid ip address<br />

}<br />

HTML Color<br />

The html color validator checks that the giv<strong>en</strong> value is a valid html hex color.<br />

// models/User.php<br />

Listing<br />

13-29<br />

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

{<br />

public function setTableDefinition()<br />

{<br />

par<strong>en</strong>t::setTableDefinition();<br />

// ...<br />

}<br />

}<br />

$this->hasColumn('favorite_color', 'string', 7, array(<br />

'htmlcolor' => true<br />

)<br />

);<br />

Here is the same example in YAML format. You can read more about YAML in the YAML<br />

Schema Files (page 195) chapter:<br />

---<br />

# schema.yml<br />

Listing<br />

13-30<br />

# ...<br />

User:<br />

columns:<br />

# ...<br />

favorite_color:<br />

type: string(7)<br />

htmlcolor: true<br />

# ...<br />

Now if you try and save a User with an invalid html color value for the favorite_color<br />

column the validation will fail.<br />

// test.php<br />

Listing<br />

13-31<br />

// ...<br />

$user = new User();<br />

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

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

Saved successfully!

Ooh no, something went wrong!