Sponsor
Now Playing
- Alix Perez – I'm Free 2 hours ago
- Alix Perez – Intersections 2 hours ago
- Alix Perez – Forsaken 3 hours ago
- Glen E Ston – Ouroboros (Original Mix) 3 hours ago
- Black Sun Empire – Everything 3 hours ago
Slug Slugfield Regex with CakePHP Validation
April 6, 2009,
194 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.
'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!
function slugify($data) {
if(empty($data['slug'])){
$slug = low(Inflector::slug($data['title'], '-'));
} else {
$slug = low(Inflector::slug($data['slug'], '-'));
}
return $slug;
}
2 Comments
Thank you for this, it’s just what I’m looking for – only it doesn’t work… It is allowing spaces in the slug.
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