The Controller file at app/controllers/post_controller.php
class PostController extends ApplicationController { function create(){ $this->Post =& new Post($this->params["post"]); if($this->Post->save()){ $this->redirectToAction("show", array('id' => $Post->getId())); }else{ $this->Post->errors = $this->Post->getErrors(); } } //... function show(){ } }
The View file at app/views/post/show.tpl
{?Post.errors}
The post couldn't be saved due to these errors:
<ul>
{loop Post.errors}
<li>{error}</li>
{end}
</ul>
{end}
<%= form "post" %>
or even shorter:
<%= error_messages_for 'post' %>
The Model file at app/models/post.php
class Post extends ActiveRecord { function validate(){ $this->validatesPresenceOf( array('title', 'body'), "Missing required field"); $this->get('title') > 10 ? $this->addError("title", "must be at most 10 characters") : null; } }
Syntax:
$this->validatesAcceptanceOf(accept $attribute_names, [ $message = 'accepted'], [ $accept = 1]);
Syntax:
$this->validatesAssociated($attribute_names, [ $message = 'invalid']);
Syntax:
$this->validatesConfirmationOf($attribute_names, [ $message = 'confirmation']);
Description:
Note: When using validatesConfirmationOf() make sure you also use validatesPresenceOf() if you want to make sure the field exists.
Syntax:
$this->validatesExclusionOf($attribute_names, $array_of_possibilities, [ $message = 'exclusion'], [ $allow_null = false]);
Syntax:
$this->validatesFormatOf($attribute_names, $regular_expression, [ $message = 'invalid'], [ $regex_function = 'preg_match']);
Syntax:
$this->validatesInclusionOf($attribute_names, $array_of_possibilities, [ $message = 'inclusion'], [ $allow_null = false]);
Syntax:
$this->validatesLengthOf($attribute_names, [ $options = array()]);
Syntax:
$this->validatesNumericalityOf($attribute_names, [ $message = 'not_a_number'], [ $only_integer = false], [ $allow_null = false]);
Syntax:
$this->validatesPresenceOf($attribute_names, [ $message = 'blank']);
Syntax:
$this->validatesSizeOf($attribute_names, [ $options = array()]);
Syntax:
$this->validatesUniquenessOf($column_name [,$options = array('message' => "has already been taken")]);
The $options are:
message - Specifies a custom error messagescope - Ensures that the uniqueness is restricted to a condition of “scope = record.scope”case_sensitive - Looks for an exact match. Ignored by non-text columns (true by default).if - Specifies a method to call or a PHP statement to evaluate to determine if the validation should occur. A method should return a boolean value; A statement should evaluate to a boolean value.
Description:
When the record is created, a check is performed to make sure that no record exists in the database with the given value for the specified attribute (column). When the record is updated, the same check is made but disregards the record itself.
Examples:
In the model, we write:
class Person extends ActiveRecord { function validate() { $this->validatesUniquenessOf('passport_number'); $this->validatesUniquenessOf('user_name', array('scope' => "account_id")); } }
It can also validate whether the value of the specified attributes are unique based on multiple scope parameters. For example, making sure that a teacher can only be on the schedule once per semester for a particular class.
class TeacherSchedule extends ActiveRecord { function validate() { $this->validatesUniquenessOf('passport_number'); $this->validatesUniquenessOf('teacher_id', array('scope' => array("semester_id", "class_id")); } }