Changeset 2088

Show
Ignore:
Timestamp:
02/20/08 14:27:58 (6 months ago)
Author:
armen
Message:

Added depends_on rule.
Update the examples controller with new validation stuff. (it's not perfect yet, but it works :P)

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/application/controllers/examples.php

    r2005 r2088  
    152152        $validation = new Validation($data); 
    153153 
    154         // Looks familiar... 
    155         $validation->set_rules(array 
    156         ( 
    157             // Format: 
    158             // key          friendly name,  validation rules 
    159             'user' => array('username',    '=trim|required[1,12]|regex[/[0-9]+/]'), 
    160             'pass' => array('password',    'required|=sha1'), 
    161             'reme' => array('remember me', 'required') 
    162         )); 
    163  
    164         // Same syntax as before 
    165         $validation->run(); 
    166  
    167         // Same syntax, but dynamcially generated wth __get() 
    168         echo $validation->error_string; 
     154        $validation->add_rules('user', 'required', 'length[1,12]')->pre_filter('trim', 'user'); 
     155        $validation->add_rules('pass', 'required')->post_filter('sha1', 'pass'); 
     156        $validation->add_rules('reme', 'required'); 
     157 
     158        $result = $validation->validate(); 
     159 
     160        var_dump($validation->errors()); 
     161        var_dump($validation->as_array()); 
    169162 
    170163        // Yay! 
  • trunk/system/libraries/Validation.php

    r2082 r2088  
    534534    } 
    535535 
     536    /** 
     537     * Rule: matches. Generates an error if the field does not depend on one or more 
     538     * other fields. 
     539     * 
     540     * @param   mixed   field name 
     541     * @param   array   field names to check dependency 
     542     * @return  bool 
     543     */ 
     544    public function depends_on($field, array $fields) 
     545    { 
     546        foreach ($fields as $depends_on) 
     547        { 
     548            if (! isset($this[$depends_on]) OR $this[$depends_on] == NULL) 
     549                return FALSE; 
     550        } 
     551 
     552        return TRUE; 
     553    } 
     554 
    536555} // End Validation