Archive for the ‘PHP’ category

A workaround for the CakePHP alphaNumeric issue

November 27th, 2009

Recently whilst building a CakePHP app on a shiny new server I noticed some unexpected behaviour when using an alphaNumeric rule to validate a model. Whatever I passed into the model returned false. That is to say that even a valid alphanumeric string threw up an exception. (if you haven’t already dived into CakePHP, you should – it’s a rapid development framework based on MVC.)

The issue seems to be tied to PHP 5.1.2 and the way regular expressions are handled, however it seems the it may appear in other configurations too. Before I get to a workaround, note I haven’t levelled the “bug” word at either PHP nor CakePHP – for all I know it could be a difference of opinion on how something should be handled. What I do know is that I would rather harness CakePHP’s flexibility to create custom model rules than try upgrading my current live server PHP installation.

Here is an example of how you should be able to implement the alphaNumeric rule using validation in CakePHP based on a simpliefied model called “User”:
class User extends AppModel

{
var $name = 'User';
var $helpers = array('Form');
var $validate = array(
'username' => array(
'loginRule-1' => array(
'rule' => 'alphaNumeric',
'last' => true,
'message' => 'Only letters and numbers allowed'
));
}

The rule in bold italics above is the one that doesn’t always work. However this can be easily rectified with a custom rule built on a regular expression seeking out valid alphanumeric strings.

To achieve this the alphaNumeric rule just needs to be replaced with this:

"rule" => array('custom', '/^[a-z0-9]*$/i'),

Problem solved.

Drag and drop reordering of database fields (sortables) with jQuery

December 7th, 2008

This tutorial explains how to display a list of items from a database that can be reordered in real time by dragging and dropping, using the jQuery library. Moreover, no page reload is required on every reorder. If you want a bigger introduction to why you may want to do this I suggest reading my tutorial on how to achieve the same functionality using scriptaculous.

Previously I have explained how to achieve this in Scriptaculous and Mootools.

» Read more: Drag and drop reordering of database fields (sortables) with jQuery

Autocompletion with Scriptaculous and Ajax.

March 9th, 2008

Demo here | Download sample files

This tutorial supercedes my previous turtorial on autocompletion by adding further tips and tricks to make your autocompletion a much better experience for your site users The tutorial is structured in the following way:

  1. Introduction: What is autocompletion and why use it?
  2. How it works.
  3. Getting scriptaculous and building the form.
  4. Building the autocomplete server.
  5. Passing back an id to our form.

» Read more: Autocompletion with Scriptaculous and Ajax.

Alternating table row colours in PHP.

March 9th, 2006

Recently I have been tinkering with Ruby on Rails and I came across a rather nice method of alternating row colours using only two lines of code and a couple of CSS rules. I have been using this method in my PHP ever since and never looked back. This is a neat trick to alternate row colors on a table using PHP.

» Read more: Alternating table row colours in PHP.