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 like User::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 like User::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.

Previous: Models - Next: Collections

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.