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 214<br />

# ...<br />

notblank: true<br />

Now if we try and save a User record with a username that is a single blank white space,<br />

validation will fail:<br />

Listing<br />

13-12<br />

// test.php<br />

// ...<br />

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

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

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

echo 'User is invalid!';<br />

}<br />

No Space<br />

The no space validator is simple. It checks that the value doesn't contain any spaces.<br />

Listing<br />

13-13<br />

// models/User.php<br />

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

{<br />

// ...<br />

public function setTableDefinition()<br />

{<br />

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

// ...<br />

}<br />

}<br />

$this->hasColumn('username', 'string', 255, array(<br />

'nospace' => true<br />

)<br />

);<br />

Listing<br />

13-14<br />

Listing<br />

13-15<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 />

# ...<br />

User:<br />

columns:<br />

username:<br />

type: string(255)<br />

nospace: true<br />

# ...<br />

Now if we try and save a User with a username that has a space in it, the validation will fail:<br />

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

$user->username = 'jon wage';<br />

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

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

Saved successfully!

Ooh no, something went wrong!