Wednesday, November 11, 2009

How to Set Validation Rules in CakePHP

Setting Validation Rules
I have told you how to add records to a database table using CakePHP here. Now, I'll try to enforce some validation rules on our model (in this case - it is the 'posts' table in our database.).
Steps to be taken:
1. Open 'post.php' file found under 'app/models'.
2. Add the following lines just below var $name = 'Post';


var $validate = array(
'title' => 'notEmpty',
'body'  => 'notEmpty'
);

3. Save the file.
4. And you are done.

Note: We have set one validation rule for each of the two fields. var $validate is a reserved keyword and it tells cake to apply the following validation rules. There are two rules. The first rule applies to 'title' field and the second rule applies to the 'body' field. 'notEmpty' is also a pre-defined validation rule in Cake. So, Cake will not allow you to add records to the 'posts' table, if either 'title' field is empty, or, 'body' field is empty. Simple enough!

Also Note: You need to learn very well how to apply validation rules to develop a robust database driven application.
So, here is the Syntax in Cake( Simple Rules)

var $validate = array('fieldName' => 'ruleName');

Example:
var $validate = array('title' => 'notEmpty');

You can specify as many rules as you want by adding as many 'fieldName => 'ruleName' to that $validate array.
Example:

var $validate = array(
'title' => 'notEmpty',
'body'  => 'notEmpty'
);



To apply validation rules with a custom message, you can use the following Syntax as well. This will help you to write ONE Rule per FIELD.
var $validate = array(
      'fieldName1' => array(
     'rule' => 'ruleName', // or: array('ruleName', 'param1', 'param2' ...)
             'required' => true,
             'allowEmpty' => false,
             'on' => 'create', // or: 'update'
             'message' => 'Your Error Message'
             )
);

To apply MULTIPLE Validation Rules per field, use the following format.

var $validate = array(
     'fieldName' => array(
 
        'ruleName1' => array(
              'rule' => 'ruleName', // extra keys like on, required, etc. go here... ),
          'ruleName2' => array(
'rule' => 'ruleName2', // extra keys like on, required, etc. go here...)
       )
);

Although the structure appears little bit cluttered with use of array(). But with little bit of exercise it does not claim your life to IMPOSE all validation rules to your table fields. At least for me, I could easily add new rules to any field using above format.

Remember:
You need to write validation rules in 'app/models/my_model_name.php file. In our case, we wrote validation rules to 'app/models/post.php file.

Here is a COMPLETE set of Validation Rules applicable for CakePHP.
And here are some more guidelines (really well written) to help you win over this Validation Rule Challenge.

To view this page, point your browser to the following location:

http://caketest.local/posts/add
Here is a screenshot of what happens when I enforce those validation rules:
















Once more, if you have any PLAN to use Cake in future, you need to be a master in framing validation rules. So, just learn this section nicely.


There is something to look at closely 
Note: The url structure of CakePHP
http://your-domain-name.com/controllers/action

In this case, we want to access add() function under PostsController.
So, controller = 'posts' action = 'add'
And the resulting url is:
http://your-domain-name.com/posts/add

In my case, it was:
http://caketest.local/posts/add

I hope it tells how easy it is to access any controllers/action in CakePHP.

Remember:
To hide any function() (Cake calls function as 'action') from direct access, precede it with ('_') underscore.
i.e., _function()   will not be accessible in a web browser.
For example, if you modify our add(); function to look like:
function _add() {
/* Function body goes here */
}
It will not be accessible using
http://caketest.local/posts/_add
Cake will display a message saying :
Private Method in PostsController

This is really useful in case you want to create a function for internal use of your application.
Once again, at least I found this underscore convention pretty simple.


Believe me! Simply follow this non-traditional CakePHP tutorial (If you agree to call it a tutorial at all!). There are lots of stuff under the hood, which will make your coding life really simple!

Take care.

No comments: