Skip to content

Instantly share code, notes, and snippets.

@twmbx
Created August 18, 2014 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save twmbx/69bb4219a1dae611eff8 to your computer and use it in GitHub Desktop.
Save twmbx/69bb4219a1dae611eff8 to your computer and use it in GitHub Desktop.
Additional validators for use with Laravel 4
<?php
// this file is meant for use with laravel 4
// it can be placed in the root of the app directory
// and included from routes.php
// validate alphabetical chars & spaces only
Validator::extend('alpha_space', function($attr, $value) {
return preg_match('/^([a-zA-Z ])+$/i', $value);
});
// validate alphabetical chars, numbers & spaces only
Validator::extend('alpha_num_space', function($attr, $value) {
return preg_match('/^([a-zA-Z0-9 ])+$/i', $value);
});
// validate alphabetical chars, dashes & spaces only
Validator::extend('alpha_dash_space', function($attr, $value) {
return preg_match('/^([a-zA-Z -])+$/i', $value);
});
// validate alpha, num, dash, underscore
Validator::extend('ands', function($attr, $value) {
return preg_match('/^[a-zA-Z0-9_-]+$/', $value);
});
// validate alpha, num, dash, space
Validator::extend('andu', function($attr, $value) {
return preg_match('/^[a-zA-Z0-9 -]+$/', $value);
});
// validate alpha, num, dash, space, underscore
Validator::extend('andsu', function($attr, $value) {
return preg_match('/^[a-zA-Z0-9_ -]+$/', $value);
});
// assert a maximum number of fields allowed
// from a multiple select form control
Validator::extend('array_max_count', function($attr, $value, $params) {
return count($value) <= $params[0];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment