Slug Slugfield Regex with CakePHP Validation

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