Yii - Further model validation

UPDATED 26-Jan-2012 !

A brief note today to add some more specific validation to your models.  As we all know - "rubbish in == rubbish out", so let's get that data nice and clean, right up front!


public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
	                 array('firstname, surname, address', 'match', 'pattern'=>'/^[\w\-\_\'\,\.]+$/'),</del>
                         array('firstname, surname, address', 'match', 'pattern'=>'/^[\\w\\-\\_\\'\\ \\,0-9\\p{L}]+$/u'),
                         array('startdate, enddate', 'date','format'=>'d/m/yyyy', 'allowEmpty'=>false),
                );
         }

The address regex pattern is using a short-code \\w which matches all alphanumeric characters including diacritics (letters with accents etc..).  Added basic puctuation, hyphen(-), underscore(_), single quote ('),  comma(,) and full-stop/period (.) , 0-9 and lastly, if there is any chance that you might use multi-byte characters like utf-8, then the unicode point {L} matches any multi-byte letter. Note the /u at the end of the regex pattern which tells regex to use multi-byte matching. This combination should allow most european and amercian names and addresses like

O'hara
Smith-Klein
Cajó
King's Cross St. Pancras
Béziers
and even åæßέ

 It may/should even allow other multi-byte combinations, but I haven't tested it on anything non-european yet!

 

Please do add ideas for more validation rules below ...

 

further reading:  Unicode Regex Patterns


Did you know you can hire me?

I take on projects of all sizes. From Consulting to large Development Projects.

If you're starting a new Yii project and would like some help to get setup and running or you need some help with a particular module or you just need someone to develop the whole dang thing, then just ask ...


Beginning Yii ...

I'm very excited about a new Yii Training course that has just been published.

The course is designed for anyone wanting to learn Yii, whether new to frameworks or converting from another.

Eight chapters, each having five videos of between 3 and 5 minutes, take you through the basics of the Yii Framework, building a web application step-by-step.

Save 40% - introductory offer $20.99 / £13.19 / €16.19 / A$20.99

Leave a Comment

twitterfacebookgooglelinkedin https://me.yahoo.com