fredyounan
1/8/2015 - 4:01 AM

validators.php

<?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];
	});