How to Validate

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;
    }
}

More information

validatesAcceptanceOf

Syntax:

$this->validatesAcceptanceOf(accept $attribute_names, [ $message = 'accepted'], [ $accept = 1]);

validatesAssociated

Syntax:

$this->validatesAssociated($attribute_names, [ $message = 'invalid']);

validatesConfirmationOf

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.

validatesExclusionOf

Syntax:

$this->validatesExclusionOf($attribute_names, 
    $array_of_possibilities, [ $message = 'exclusion'], [ $allow_null = false]);

validatesFormatOf

Syntax:

$this->validatesFormatOf($attribute_names, 
    $regular_expression, [ $message = 'invalid'], [ $regex_function = 'preg_match']);

validatesInclusionOf

Syntax:

$this->validatesInclusionOf($attribute_names, 
    $array_of_possibilities, [ $message = 'inclusion'], [ $allow_null = false]);

validatesLengthOf

Syntax:

$this->validatesLengthOf($attribute_names, [ $options = array()]);

validatesNumericalityOf

Syntax:

$this->validatesNumericalityOf($attribute_names, 
    [ $message = 'not_a_number'], [ $only_integer = false], [ $allow_null = false]);

validatesPresenceOf

Syntax:

$this->validatesPresenceOf($attribute_names, [ $message = 'blank']);

validatesSizeOf

Syntax:

$this->validatesSizeOf($attribute_names, [ $options = array()]);

validatesUniquenessOf

Syntax:

$this->validatesUniquenessOf($column_name [,$options = array('message' => "has already been taken")]);

The $options are:

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")); 
    }
}