Advanced components - Validators - More Validation.php 2.69 KB
Newer Older
1
The type and length validations are handy but most of the time they're not enough. Therefore
hansbrix's avatar
hansbrix committed
2 3 4 5
Doctrine provides some mechanisms that can be used to validate your data in more detail.



6 7 8 9 10
Validators: Validators are an easy way to specify further validations. Doctrine has a lot of predefined
validators that are frequently needed such as email, country, ip, range and regexp validators. You
find a full list of available validators at the bottom of this page. You can specify which validators
apply to which column through the 4th argument of the hasColumn() method.
If that is still not enough and you need some specialized validation that is not yet available as
hansbrix's avatar
hansbrix committed
11 12 13 14 15 16 17 18 19 20 21 22
a predefined validator you have three options:



- You can write the validator on your own.

- You can propose your need for a new validator to a Doctrine developer.

- You can use validation hooks.



23 24
The first two options are advisable if it is likely that the validation is of general use
and is potentially applicable in many situations. In that case it is a good idea to implement
hansbrix's avatar
hansbrix committed
25 26 27 28 29 30 31 32 33 34 35 36
a new validator. However if the validation is special it is better to use hooks provided by Doctrine:



- validate() (Executed every time the record gets validated)

- validateOnInsert() (Executed when the record is new and gets validated)

- validateOnUpdate() (Executed when the record is not new and gets validated)



37 38 39
If you need a special validation in your active record
you can simply override one of these methods in your active record class (a descendant of Doctrine_Record).
Within thess methods you can use all the power of PHP to validate your fields. When a field
40 41
doesnt pass your validation you can then add errors to the record's error stack.
The following code snippet shows an example of how to define validators together with custom
hansbrix's avatar
hansbrix committed
42 43
validation:

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

<code type="php">
class User extends Doctrine_Record {
    public function setUp() {
        $this->ownsOne("Email","User.email_id");
    }
    public function setTableDefinition() {
        // no special validators used only types 
        // and lengths will be validated
        $this->hasColumn("name","string",15);
        $this->hasColumn("email_id","integer");
        $this->hasColumn("created","integer",11);
    }
    // Our own validation
    protected function validate() {
        if ($this->name == 'God') {
            // Blasphemy! Stop that! ;-)
            // syntax: add(<fieldName>, <error code/identifier>)
            $this->getErrorStack()->add('name', 'forbiddenName');
        }
    }
}
class Email extends Doctrine_Record {
    public function setTableDefinition() {
        // validators 'email' and 'unique' used
        $this->hasColumn("address","string",150, array("email", "unique"));
    }
}  
</code>