Slug Slugfield Regex with CakePHP Validation

Meta: April 06th 2009 // CakePHP // 130 views

Not a very special post, however i simply wanted to make future reference for myself with my CakePHP custom regex validation for slug fields.

1
2
3
4
'slug' => array(
'RegEx' => array(
'rule' => array('custom', '/^[a-zA-Z0-9-\s_]+$/i'),
'message' => 'Slugs must only contain letters, numbers, underscores and hyphens "-"'),

Also here is my function to convert my titles to a slug :) Using CakePHP of course!

1
2
3
4
5
6
7
8
function slugify($data) {
if(empty($data['slug'])){
$slug = low(Inflector::slug($data['title'], '-'));
} else {
$slug = low(Inflector::slug($data['slug'], '-'));
}
return $slug;
}

Tags: , ,

WeDecal.com

Postscript: Leave A Comment // Subscribe (RSS Feed)

Comments About Slug Slugfield Regex with CakePHP Validation

// 2 comments so far.

  1. Daniel // March 10th 2010

    Thank you for this, it’s just what I’m looking for – only it doesn’t work… It is allowing spaces in the slug.

  2. VoiDeT // March 10th 2010

    Seems as though there is a problem in my regex, just change:

    ‘rule’ => array(‘custom’, ‘/^[a-zA-Z0-9-\s_]+$/i’),

    to:

    ‘rule’ => array(‘custom’, ‘/^[a-zA-Z0-9-_]+$/i’),

    That should make the validation trip :)

Who Are You?

Your Email Address

Your Website

:D :) :o :eek: :( :lol: :wink: :arrow: :idea: :?: :!: :evil: :p

You can follow any responses to this entry via its RSS comments feed. You may also leave a trackback by clicking this link.