[ create a new paste ] login | about

Link: http://codepad.org/8YWbV1Gp    [ raw code | fork ]

ninwa - PHP, pasted on May 3:
<?php
	function validate_array($a, $types, $validators, &$errors){
		foreach(array_keys($types) as $key){
			if(isset($a[$key])){
				if(isset($validators[$types[$key]]) &&
					is_callable($validators[$types[$key]][0])){
					if( $validators[$types[$key]][0]($a[$key]) == false ){
						if(isset($validators[$types[$key]][1])){
							$errors[] = sprintf($validators[$types[$key]][1], $key);
						} else {
							$errors[] = $key . ' is invalid somehow.';
						}
					}
				} else {
					$errors[] = $key . ' has either no, or an invalid validation rule.';
				}
			} else {
				$errors[] = $key . ' is not a valid key.';
			}
		}
	}
	               
	$validators = Array('digit' => Array('ctype_digit', 
	                                     '%s must be a number'),
	                    'alpha' => Array('ctype_alpha', 
	                                     '%s must be only alpha characters.'));
	$types = Array('tireqty' => 'digit',
	               'oilqty'  => 'digit',
	               'plugqty' => 'digit' );
	
	$errors = Array();
	validate_array($_POST, $types, $validators, $errors);


Output:
No errors or program output.


Create a new paste based on this one


Comments: