Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2006
    Posts
    5,477
    Plugin Contributions
    11

    Default From developer to developer ^_^ -- validation class

    Here is a zencart's class I have been using to validate user submitted data

    PHP Code:
    <?php

       
    class _yvalidation{
            
    // TODO: use 'private' when ZC moves to PHP5
            
    var $default_messages = array(    'validation_is_not_empty'     => 'is empty',
                                            
    'validation_is_number'     => 'is not a number',
                                            
    'validation_is_positive'     => 'is not a positive number',
                                            
    'validation_is_email'         => 'is not a valid email address',
                                            
    'validation_is_date'        => 'is not a valid date',
                                            
    'validation_is_url'        => 'is not a valid url');
        
            var 
    $rules;
            var 
    $params = array();
            var 
    $error_count 0;
            var 
    $validationErrors = array();
            var 
    $class 'validation';
            
    // class is used to later display error message
            // if ($messageStack->size(class) > 0) echo $messageStack->output(class);
            
    function set_error_class($class){
                
    $this->class $class;
            }
            
            function 
    set_rules($rules){
                
    $this->rules $rules;
            }
            
            function 
    add_rules($rules){
                foreach(
    $rules as $key => $value){
                    if(
    array_key_exists($key$this->rules))
                        
    $this->rules[$key][] = $value;
                    else
                        
    $this->rules[$key] = $value
                }        
            }
            
            function 
    set_messages($messages){
                
    $this->default_messages $messages;
            }
            
            function 
    add_messages($messages){
                
    $this->default_messages[] = $messages;
            }
            
            function 
    set_params($params){
                
    $this->params $params;
            }
            
            function 
    add_params($params){
                
    $this->params[] = $params;
            }
        
            function 
    validate_fields($params){
                foreach(
    $this->rules as $field => $rules){
                    if(isset(
    $params[$field]) && is_array($params[$field])){
                        
    $this->validate_fields($params[$name]);
                    }
                    else{
                        foreach(
    $rules as $method => $message){
                            if(
    method_exists($this$method)){
                                if(!
    call_user_func_array(array($this$method), $params[$field])){ // value of the field is passed as the rule's parameters
                                    // sounds off the alarm    
                                        
    if(isset($message) && !empty($message))
                                            
    $this->set_error($message$this->class);
                                        else
                                            
    $this->set_error("$field ".$this->default_messages[$method], $this->class);
                                }
                            }
                            else{
                                
    $this->set_error(sprintf("Could not call function '%s' to validate field '%s' with the value '%s'",$method,$field,$params[$field]), $this->class);
                                
    // sounds off the alarm    
                            
    }
                        }
                    }
                }
            }
                
            
            function 
    set_error($message$class$type='error'){
                
    $this->validationErrors[] = array('message' => $message'class' => $class'type' => $type);
                
    $this->error_count++;
            }
            
            
            function 
    set_zen_error($is_admin_side){
                global 
    $messageStack;
                foreach(
    $this->validationErrors as $error)
                    if(!
    $is_admin_side$messageStack->add($error['class'], $error['message'], $error['type']);
                    else 
    $messageStack->add($error['message'], $error['type']);
            }
            
            function 
    run($is_admin_side false){
                
    $this->validate_fields($this->params);
                
    $this->set_zen_error($is_admin_side);
                return 
    $this->error_count;
            }
            
            
    // TODO: Let the user define new validation functions somewhere else.
            // Edit/Add new validation functions here:
            
    function validation_is_not_empty($param){
                return !empty(
    $param);
            }
            
            function 
    validation_is_number($param){
                return 
    is_numeric($param);
            }
            
            function 
    validation_is_positive($param){
                if(
    $this->validation_is_number($param))
                    return (int)
    $param 0;
                return 
    false;
            }
            
    //YYYY-MM-DD
            
    function validation_is_date($date){
                if (
    ereg ("([0-9]{4})-([0-9]{2})-([0-9]{2})"$date$regs))
                    if((int)
    $regs[3]<=31 && (int)$regs[3]>0// check date
                         
    if((int)$regs[2]<=12 && (int)$regs[2]>0// check month
                             
    if((int)$regs[1]>0// check year
                                 
    return true;
                return 
    false;        
            }    
            
            function 
    validation_is_email($email){
                return 
    zen_validate_email($email);
            }
            
            function 
    validation_is_url($url){
                
    $urlregex "^(https?|ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
                if (
    eregi($urlregex$url)) 
                return 
    true;
                else 
                return 
    false;  
            }
            
    // End of editable zone
        
    }
    Usage:
    PHP Code:
            $yvalidation->set_params($_POST);
            
    $rules = array('customers_email_address'    => array('validation_is_not_empty'=>'You have to fill in your email address',
                                                             
    'validation_is_email'=>'You email address is not valid'),
                               
    'customers_name'    => array('validation_is_not_empty'=>'You have to fill in your contact name'),
                               
    'customers_needs'            => array('validation_is_not_empty'=>'You have to fill in your inquiry message'),
                               
    'customers_inquiry_topic'    =>array('validation_is_not_empty'=>''),
                               
    'customers_inquiry_message'    =>array('validation_is_not_empty'=>'Please fill in your inquiry'),
                               );
            
    $yvalidation->set_rules($rules);
            
    $error_counter $yvalidation->run();
            if(
    $error_counter == 0){
    // do something

    I have found it to be very useful for me, you can write another class to extend this class then put your own validation methods in.
    Just simply set the fields you want to validate, the error message (or you can set default error message as well), and the class will do all the validation for you.

    Hope this helps someone.
    I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

  2. #2
    Join Date
    Mar 2009
    Posts
    19
    Plugin Contributions
    0

    Default Re: From developer to developer ^_^

    Hi,

    I was wondering where would I able this information too?

    Thanks,
    Dat

  3. #3
    Join Date
    Feb 2006
    Location
    New Zealand
    Posts
    28
    Plugin Contributions
    2

    Default Re: From developer to developer ^_^ -- validation class

    ZenMagick includes a validation framework, based on rules. The beauty of this is that those rules (each a separate class) do not only validate on the server side, but can also generate JavaScript code to validate in the browser.

    That way, client and server validation are always in sync and can be configured in a single place and without having to change code!

    Example: Rules for the change password form:

    $validator->addRules('account_password', array(
    array('RequiredRule' ,'password_current', 'Please enter you current password.'),
    array('RequiredRule' ,'password_new', 'Please enter the new password.'),
    array('MinRule' ,'password_new', ZMSettings::get('minPasswordLength'), 'Your password must contain a minimum of %2$s characters.'),
    array('RequiredRule' ,'password_confirmation', 'Please confirm the new password.'),
    array('FieldMatchRule' ,'password_new', 'password_confirmation', 'The new password and confirm password must match.')
    ));

    @yellow: We seem to have a very similar mindset - trying to find solutions for common problems ...

    Cheers, mano

 

 

Similar Threads

  1. IE9 Developer Tools HTML1113: Document mode restart from Quirks to IE9 Standards
    By torvista in forum Templates, Stylesheets, Page Layout
    Replies: 0
    Last Post: 13 Jul 2011, 10:18 PM
  2. Combination of old and new sites on server after uploading files from my developer
    By billybjr in forum Installing on a Linux/Unix Server
    Replies: 3
    Last Post: 31 May 2010, 12:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg