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 ...


Jumpstart Yii2 ... Coming Soon!

I'm in the process of planning a new course on Yii2 and want your feedback.

What is your preferred format? What did you think of my Yii 1.x video course "Beginning Yii"?

What would you like to see different?

How much would you pay?

Leave a Comment

twitterfacebookgooglelinkedin https://me.yahoo.com