Page 1 of 1

Minimal input validator class

Posted: Thu Oct 01, 2009 3:24 am
by lorenzo-s
Hi everyone. Yesterday I've coded a simple input validator class. The goal is to quickly check forms input values and store error messages for each one. That's it:

Code: Select all

    class Validator {
        
        // Error list
        private $errors = array();
        
        // Validating options
        const NOT_EMPTY = 1;
        const NUMERIC   = 2;
        const INTEGER   = 4;
        const UNSIGNED  = 8;
        const ALPHA_NUM = 16;
        const EMAIL     = 32;
        const REG_EXPR  = 64;
        
        /** Validate a valu ewith specified options and set error on fail
         * @param $key Identifier of the value
         * @param $value Value to check
         * @param $message Error message to set
         * @param $options Validating options
         * @param $reg_exp Optional regular expression, if Validator::REG_EXPR is int the options */
        public function check($key, $value, $message, $options = self::NOT_EMPTY, $reg_expr = null) {
            
            // Check for numeric value
            if ($options & self::NUMERIC) if (
                (!String::isNumeric($value)) ||
                (($options & self::NOT_EMPTY) && ($value == 0)) ||
                (($options & self::UNSIGNED ) && ($value <  0))
            ) {
                $this->errors[$key] = $message;
                return;
            }
            
            // Check for integer value
            if ($options & self::INTEGER) if (
                (!String::isInteger($value)) ||
                (($options & self::NOT_EMPTY) && ($value == 0)) ||
                (($options & self::UNSIGNED ) && ($value <  0))
            ) {
                $this->errors[$key] = $message;
                return;
            }
            
            // Check for alphanumeric strings
            if ($options & self::ALPHA_NUM) if (
                (!String::isAlphanum($value))
            ) {
                $this->errors[$key] = $message;
                return;
            }
            
            // Check for valid email
            if ($options & self::EMAIL); // TODO email check with preg_match()
            
            // Check with regular expression
            if ($options & self::REG_EXPR); // TODO check with preg_match() with $reg_expr
            
            // Check empty string, or null, or false value (not 0 or "0"!)
            if ($options & self::NOT_EMPTY) if (
                ($value === '') ||
                ($value === null) ||
                ($value === false)
            ) {
                $this->errors[$key] = $message;
                return;
            }
            
        }
        
        /** Returns TRUE if there are no errors */
        public function isOk() {
            return (empty($this->errors));
        }
        
        /** Get the error list as associative array */
        public function getErrors() {
            return $this->errors;
        }
        
        /** Reset the error list */
        public function reset() {
            $this->errors = array();
        }
        
    } 
I will use it in that way:

Code: Select all

    // Check POST
    $val = new Validator();
    $val->check('name', $_POST['name'], 'Insert your name here', Validator::NOT_EMPTY);
    $val->check('mail', $_POST['mail'], 'Type a correct email address', Validator::EMAIL | Validator::NOT_EMPTY);
    $val->check('age' , $_POST['age' ], 'Invalid age!', Validator::INTEGER | Validator::UNSIGNED);
    
    // Get errors
    if (!$val->isOk()) $errors = $val->getErrors();
    else {
        // Do stuff on correct values
    }
    
    // Before each HTML form fields
    if ($errors['name']) echo '<p class="error">' . htmlentities($errors['name']) . '</p>';
What do you think about it? Too much simple? Too much difficult to use?? :D

Re: Minimal input validator class

Posted: Thu Oct 01, 2009 5:30 pm
by Christopher
Simpler might be:

Code: Select all

    // Check POST
    $val = new Validator($_POST);
    $val->checkEmpty('name', 'Insert your name here');
    $val->checkEmail('mail', 'Type a correct email address');
    $val->checkInteger('age' , 'Invalid age!');
I'd prefer it was not monolithic, but for a simple solution this looks good.

Re: Minimal input validator class

Posted: Fri Oct 02, 2009 11:58 am
by lorenzo-s
Pass the values array in the Validator constructor it's a good idea!
About the multiple function, yes, it's shorter and easier, but if I want to check that an integer is unsigned, non zero and satisfy a regular expression, I have to call four function instead of one...

Re: Minimal input validator class

Posted: Fri Oct 02, 2009 7:30 pm
by Christopher
lorenzo-s wrote:About the multiple function, yes, it's shorter and easier, but if I want to check that an integer is unsigned, non zero and satisfy a regular expression, I have to call four function instead of one...
For more complex validation, I would recommend having a separate classes for each rule. Then you can pass several rule objects for a single field.