-
Notifications
You must be signed in to change notification settings - Fork 30
Validations
Eustáquio Rangel edited this page Jun 10, 2013
·
8 revisions
Previous: Models - Next: Collections
Some validations are provided:
-
Presence (not empty):
User::validates("name" ,array("presence"=>true))
-
Regular expression:
User::validates("email",array("format" =>"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"))
There are options to allow blank and null values:"allow_blank"=>true
and"allow_null"=>true
, after the "format" option likeUser::validates("zip",array("format"=>"^[0-9]{5}-[0-9]{3}$","allow_null"=>true))
-
Uniqueness:
User::validates("email",array("uniqueness"=>true))
There are options to allow blank and null values:"allow_blank"=>true
and"allow_null"=>true
, after the "uniqueness" option likeUser::validates("email",array("uniqueness"=>true,"allow_null"=>true))
-
Numericality:
User::validates("id",array("numericality"=>true))
You can check for an valid object using $user->isValid()
.
For multiple validations on the same attribute, just add a validates
line for each validation.