Jump to content

Better Validation


Recommended Posts

I was tired of writing something like

if (Validate::IsInt($var) || Validate::[...]($var) || Validate::[...]($var)){}

So here's an easier way (note that it requires PHP version 5.3 or higher and changes to core files, if you're on 1.4, just add this snippet to overrides). In your classes/Validate.php add the following method:

public static function __callStatic($method, $arguments)
$argument   = array_pop($arguments);
$method     = explode('_or_', $method);
$validStack = array();

foreach ($method as $validate)
	if (method_exists('Validate', $validate))
		array_push($validStack, $validate);

if ( ! sizeof($validStack))
	die(Tools::displayError('No valid methods were passed'));

foreach ($validStack as $methodToPass)
	if ( ! call_user_func(array('Validate', $methodToPass), $argument) === false)
		return true;

return false;

Now you can do this:

if (Validate::isInt_or_isFloat_or_isEmail($var)){}

Have fun :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...