This recipe shows how to not only use some basic validation rules provided by CakePHP, but also how to use more than one of these rules per field.
To go through this recipe we need a sample table to work with. Create a table named profiles
using the following SQL statement:
CREATE TABLE `profiles`( `id` INT UNSIGNED AUTO_INCREMENT NOT NULL, `email` VARCHAR(255) NOT NULL, `name` VARCHAR(255) default NULL, `twitter` VARCHAR(255) default NULL, PRIMARY KEY(`id`) );
We proceed now to create the required model. Create the model Profile
in a file named profile.php
and place it in your app/models
folder with the following contents:
<?php class Profile extends AppModel { public $validate = array( 'email' => array('rule' => 'notEmpty'), 'name' => array('rule' => 'notEmpty') ); } ?>
Create its appropriate controller ProfilesController
in a file named profiles_controller.php
and place it in your app/controllers
folder with the following...