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; } |




Comments About Slug Slugfield Regex with CakePHP Validation
// 2 comments so far.
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.
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
You can follow any responses to this entry via its RSS comments feed. You may also leave a trackback by clicking this link.