Posts Tagged ‘CakePHP’

Dr Martens For Life Microsite

Sunday, March 15th, 2009

Haven’t updated the blog for a while so I thought I would create a few posts on what I have been up to in the early stages of 2009!

Back in February, I developed a microsite for Dr Martens, named ‘Dr Martens For Life’. The whole idea behind the site is to activate your guarantee for your Dr Martens Boots / Shoes for life!

Dr Martens For Life Microsite

Using CakePHP, I did some lovely custom validation to check if a guarantee code was valid (from a list of codes in a MySQL table) and also to see if it had not been redeemed before (only one code per product).

Take a look, if you fancy buying a pair of Dr Martens - take a look here too.

shhh.tv

Tuesday, December 23rd, 2008

Looking for a special gift for someone but cannot think of what?

Well shhh.tv is here to sort that!! simply, answer a few questions and generate a gift for a friend or foe… its a shhhhhecret!

shhh.tv screenshot

shhh.tv is a MAKE project for LOVE.

I am proud to say that I built the ‘back-end’ of the site…. ooooh err, using the CakePHP framework, MySQL and AMFPHP.

Enjoy and Merry Christmas!

Custom Validation Rules in CakePHP 1.2

Thursday, November 6th, 2008

CakePHP’s validation class is pretty powerful, however there may be a time when you require a validation method that hasn’t been defined, this is where custom validation rules fit in.

Lets imagine you have a select box containing a users ‘title’ field in a form. You also have a ‘other_title’ text field which must be filled in if the ‘Other’ value is selected.

How would you go about validating this?

Simple: In your Model specify your validation criteria as so…

var $validate = array(
'title' => array(
'rule' => array('minLength',1),
'message' => 'Please select your title'
),
'other_title' => array(
'rule' => array('_compareFields', 'title', 'other'),
'message' => 'Please enter your other title'
)
);

As you can see when the Model validates, the ‘other_title’ field is validated against a custom rule named ‘_compareFields’ parsing in ‘title’ and ‘other’ as parameters.

‘title’ is the field to compare ‘title_other’ with and ‘other’ is the compare value that I am matching on.
Therefore the custom rule looks like:

function _compareFields($field=array(), $compare_field, $compare_value){
if(!empty($this->data[$this->name][$compare_field])&&(strtolower($this->data[$this->name][$compare_field]) == strtolower($compare_value))) {
$field_value = array_shift($field);
if(!empty($field_value)) {
return true;
} else {
return false;
}
} else {
return true;
}
}

I could of simply used ‘Other’ as the $compare_value but I like writing generic functions that can be re-used multiple times. This function will now cater for other fields with different $compare_fields and the ‘case’ of this value is not sensitive in the view.

Hope that helps someone out there!

Mark Holt

This is the blog of Mark Holt, a Manchester based web developer currently working for LOVE.


Search

Categories