Changeset 1441

Show
Ignore:
Timestamp:
12/07/2007 12:02:27 PM (10 months ago)
Author:
Shadowhand
Message:

Fixing some bugs with validation in Form_Input.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/forge/libraries/Form_Input.php

    r1439 r1441  
    4242                if ($method == 'rules') 
    4343                { 
    44                         $this->add_rules(explode('|', $args[0])); 
     44                        // Set rules and action 
     45                        $rules  = $args[0]; 
     46                        $action = substr($rules, 0, 1); 
     47 
     48                        if (in_array($action, array('-', '+', '='))) 
     49                        { 
     50                                // Remove the action from the rules 
     51                                $rules = substr($rules, 1); 
     52                        } 
     53                        else 
     54                        { 
     55                                // Default action is replace 
     56                                $action = '='; 
     57                        } 
     58 
     59                        $this->add_rules(explode('|', $rules), $action); 
    4560                } 
    4661                elseif ($method == 'name') 
     
    106121        } 
    107122 
    108         protected function add_rules( array $rules) 
    109         { 
     123        protected function add_rules( array $rules, $action = '=') 
     124        { 
     125                if ($action === '=') 
     126                { 
     127                        // Just replace the rules 
     128                        $this->rules = $rules; 
     129                        return; 
     130                } 
     131 
    110132                foreach($rules as $rule) 
    111133                { 
    112                         if ( ! in_array($rule, $this->rules)) 
    113                         { 
    114                                 $this->rules[] = $rule; 
     134                        if ($action === '-') 
     135                        { 
     136                                if ($key = array_search($rule, $this->rules)) 
     137                                { 
     138                                        // Remove the rule 
     139                                        unset($this->rules[$key]); 
     140                                } 
     141                        } 
     142                        else 
     143                        { 
     144                                if ( ! in_array($rule, $this->rules)) 
     145                                { 
     146                                        if ($action == '+') 
     147                                        { 
     148                                                array_unshift($this->rules, $rule); 
     149                                        } 
     150                                        else 
     151                                        { 
     152                                                $this->rules[] = $rule; 
     153                                        } 
     154                                } 
    115155                        } 
    116156                } 
     
    245285        protected function rule_length($min, $max = NULL) 
    246286        { 
    247                 // Get the length 
    248                 $length = strlen($this->value); 
     287                // Get the length, return if zero 
     288                if (($length = strlen($this->value)) === 0) 
     289                        return; 
    249290 
    250291                if ($max == NULL)