Creating a simple form and custom validation rules in CakePHP 1.2


Note: In CakePHP 1.2, form related methods have been deprecated from the HtmlHelper and moved to the new FormHelper. Now we have to use FormHelper instead of HtmlHelper. Also the tagErrorMsg is removed in the current version.

Our Controller Class:
For validating the given input, we need to write this in our controller function:

class PagesController extends AppController {
var $name = 'Pages';
var $helpers = array('Html', 'Form');
var $uses = array('Category');

function add() {
if (isset($this->data)) {
$this->Category->data = $this->data;   //assigns the input data to the corresponding model.
if ($this->Category->validates($this->data)) { //Checks whether validation is done or not
 /*code here*/
} else {
/*code here*/

Creating a simple form in CakePHP 1.2

Let our view be views/pages/add.ctp

/// start php tag
echo $form->create('', array('name' => 'frm', 'id' => 'frm', 'method' => 'post', 'action' => 'add'));
/// end php tag
/// start php tag echo $form->label('category_name', 'Category Name: '); /// end php tag /// start php tag echo $form->input('Category.category_name', array('type'=>'text', 'label'=>false, 'div'=>false)); /// end php tag /*'$form->input, by default creates a label for the input and the input box, if we set 'label' option to 'false', then it will not create the label for the input, as here we have seperately created the label in the previous : label('category_name', 'Category Name: ');?> */
  /// start php tag echo $form->submit('Submit'); /// end php tag
/// start php tag echo $form->end(); /// end php tag

Data validation in cakePHP 1.2

Suppose there is a table ‘categories’. We want to validate the ‘category_name’ field. Category name should not be empty and the length of the name has to be 5 to 10 characters.
Then we have to make a model Category.php :

class Category extends AppModel {
var $name = "Category";
var $validate = array(
       'category_name' => array(
            'required' => array(
                'rule'=>VALID_NOT_EMPTY,'message'=>'Please enter category name'),
                'length' => array(
                    'rule' => 'validateLength', 
                    'min' => 5, 
                    'max' => 10,
                    'message'=>'Category name must be of 5-10 characters.') 

function validateLength($value, $params = array()) {
$valid = false;
if ($value['category_name'] != '') {
if (strlen($value['category_name']) < $params['min'] || strlen($value['category_name']) > $params['max']) {
$valid = false;
} else {
$valid = true;
} else {
$valid = true;
return $valid;

Now if we leave the category name field blank, and press Submit button, it will give the error message ‘Please enter category name’. If we enter less than 5 characters or more than 10 characters in the category name field, it will show the error message ‘Category name must be of 5-10 characters.

This entry was posted in CakePHP, PHP and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *