How To Validate

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:

  • message - Specifies a custom error message
  • scope - 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.
    • 'if' ⇒ 'allowValidation'
    • 'if' ⇒ '$this→signup_step > 2')

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")); 
    }
}
 
how-to-validate.txt · Last modified: 2009/01/24 02:50 by 82.103.203.179
 

The Akelos Framework was created by Bermi Ferrer and other contributors.
Potions of the code and documentation have been ported from Ruby on Rails.

The Akelos Framework is released under the LGPL license.

"Akelos", "Akelos Framework", and the Akelos logo are trademarks of Bermi Labs All rights reserved.

Wiki driven by DokuWiki