Posts Tagged ‘cakephp’

User Defined Validation in CakePHP 1.2

I am really enjoying the flexibility that CakePHP 1.2 gives when it comes to validating forms. While there is a decent list of validation rules right out of the box, creating your own is pretty easy using the userDefined method. Thanks to Daniel and his article at www.4webby.com/blog for turning me on to this method. Since I like and learn from code examples, I’m going to use validating a name field as an example in this article. Here are the rules I would like to achieve:

  1. Alphanumeric characters are allowed. Numbers are allowed for names like Fred Jones the 2nd.
  2. White space must be allowed.
  3. The period character must be allowed.
  4. The name field cannot be empty
  5. The name field must be a minimum of 3 characters or more.

That said I would also like different messages printed for each in the following order:

  1. For leaving a field empty.
  2. For not entering 3 or more characters in the field.
  3. For entering non alphanumeric, white space, or the period character.

Setting It Up

Setting up multiple rules in the validation attribute is done like this:
public $validate = array(
'name' => array(
'alphaNumericws'=>array(
'rule'=>array('userDefined', 'MyModel', 'wAlphaNumeric'),
'message'=>'Only alphabets, numbers, white space, and the period characters are allowed.'
),
'minlength'=>array(
'rule'=>array('minLength', '3'),
'message'=>'Minimum length of 3 characters.'
),
'notempty'=>array(
'rule'=>array('userDefined', 'MyModel', 'notEmpty'),
'message'=>'Please enter in your name.'
)
)

The syntax for using userDefined goes like this:
'rule'=>array('userDefined', 'model_class_where_defined_method_is_located', 'methodName')
In the example I’ve added 2 of my own defined validation methods: wAlphaNumeric and notEmpty. While CakPHP already has an alphaNumeric method it doesn’t let you white list any characters. In this case I wanted to white list ‘white space’ and the period character to knock out rules 1, 2, and 3 from the list above. Here is the corresponding method:
/** check alpha numeric but white space is allowed */
public function wAlphaNumeric($check, $params) {
$valid = false;
$pattern = "^([a-zA-Z0-9\ \.]+)$";
if(ereg($pattern, $check, $test)) {
$valid = true;
}
return $valid;
}

My second user defined rule is nothing more than a replacement for VALID_NOT_EMPTY in CakePHP 1.1. I read that this has been marked to be discontinued and did not want to use it. Here is the method:
/** VALID_NOT_EMPTY replacement */
public function notEmpty($check, $params) {
$valid = false;
if(!empty($check)) {
$valid = true;
}
return $valid;
}

Here is the full code example:
public $name = 'MyModel';
public $validate = array(
'name' => array(
'alphaNumericws'=>array(
'rule'=>array('userDefined', 'MyModel', 'wAlphaNumeric'),
'message'=>'Only alphabets, numbers, white space, and the period characters are allowed.'
),
'minlength'=>array(
'rule'=>array('minLength', '3'),
'message'=>'Minimum length of 3 characters.'
),
'notempty'=>array(
'rule'=>array('userDefined', 'MyModel', 'notEmpty'),
'message'=>'Please enter in your name.'
)
)
);

/** check alpha numeric but white space is allowed */
public function wAlphaNumeric($check, $params) {
$valid = false;
$pattern = "^([a-zA-Z0-9\ \.]+)$";
if(ereg($pattern, $check, $test)) {
$valid = true;
}
return $valid;
}
/** VALID_NOT_EMPTY replacement */
public function notEmpty($check, $params) {
$valid = false;
if(!empty($check)) {
$valid = true;
}
return $valid;
}

Using the userDefined() method is extremely flexible. Another cool thing about doing it this way is that I can share the validation methods in other models.

CakePHP 1.2 – No Longer in Alpha

It looks like CakePHP 1.2 is now officially fully beta. While this is old news that was announced at the beginning of the year, I simply did not get around to updating until today. My applications where currently using the alpha-beta release. Updating was a cinch and everything went well. No errors, updating configs, or changing any code.

I’ve been really looking forward to seeing 1.2 reach beta. My hats off to the development team for making such a great framework.

CakePHP Podcast

I was skimming over @TheKeyboard and saw that their is a new podcast called the Show for CakePHP which will be a CakePHP-specific podcast. So far I’ve listened to the first podcast and the material is very informative. I also appreciate that they are covering a lot of information concerning the use of CakePHP 1.2. I’ve switch to 1.2 on my last applications and have found out that it can be daunting task figuring out how to use some of it cool new features. I’ll definantly listen to their future broadcast.

Trying some cake

I’ve been playing around with CakePHP for the last couple of days now and so far I’m impressed. Of course I’ve got to jump the hurdle of learning a framework but CakePHP seems fairly straight forward. At the moment I’m trying to get use to using the AJAX/JavaScript instead of doing this without the framework. Here are some links I’ve found to be useful in getting started:

Return top

About Me

I am a husband, father of 5, opportunistic entrepreneur / freelancer, and IT professional. I can usually be found hanging out with his wife and kids, playing the role of system administrator, or developing web applications.