Changeset 2618

Show
Ignore:
Timestamp:
05/02/08 13:16:13 (2 months ago)
Author:
Shadowhand
Message:

Added array support to Validation. By using field.*, you can apply rules to every value in an array. Callbacks applied to arrays must be manually handled by using Kohana::key_string()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/system/libraries/Validation.php

    r2590 r2618  
    1515        protected $any_field; 
    1616 
     17        // Array fields 
     18        protected $array_fields = array(); 
     19 
    1720        // Filters 
    1821        protected $pre_filters = array(); 
     
    164167                foreach ($fields as $field) 
    165168                { 
     169                        if (strpos($field, '.') > 0) 
     170                        { 
     171                                // Field keys 
     172                                $keys = explode('.', $field); 
     173 
     174                                // Add to array fields 
     175                                $this->array_fields[$field] = $keys[0]; 
     176                        } 
     177 
    166178                        // Add the filter to specified field 
    167179                        $this->pre_filters[$field][] = $filter; 
     
    200212                foreach ($fields as $field) 
    201213                { 
     214                        if (strpos($field, '.') > 0) 
     215                        { 
     216                                // Field keys 
     217                                $keys = explode('.', $field); 
     218 
     219                                // Add to array fields 
     220                                $this->array_fields[$field] = $keys[0]; 
     221                        } 
     222 
    202223                        // Add the filter to specified field 
    203224                        $this->post_filters[$field][] = $filter; 
     
    259280                        $rule = (is_string($rule) AND strpos($rule, '::') !== FALSE) ? explode('::', $rule) : $rule; 
    260281 
     282                        if (strpos($field, '.') > 0) 
     283                        { 
     284                                // Field keys 
     285                                $keys = explode('.', $field); 
     286 
     287                                // Add to array fields 
     288                                $this->array_fields[$field] = $keys[0]; 
     289                        } 
     290 
    261291                        // Add the rule to specified field 
    262292                        $this->rules[$field][] = array($rule, $args); 
     
    297327 
    298328                        $callback = (is_string($callback) AND strpos($callback, '::') !== FALSE) ? explode('::', $callback) : $callback; 
     329 
     330                        if (strpos($field, '.') > 0) 
     331                        { 
     332                                // Field keys 
     333                                $keys = explode('.', $field); 
     334 
     335                                // Add to array fields 
     336                                $this->array_fields[$field] = $keys[0]; 
     337                        } 
    299338 
    300339                        // Add the callback to specified field 
     
    333372                        } 
    334373 
    335                         // Make sure all fields are defined 
    336                         isset($this[$field]) or $this[$field] = NULL; 
     374                        if (isset($this->array_fields[$field])) 
     375                        { 
     376                                // Use the first key for the field 
     377                                $field_key = $this->array_fields[$field]; 
     378 
     379                                // Load the field as an array 
     380                                $this[$field_key] = (array) Kohana::key_string($field, $this); 
     381                        } 
     382                        else 
     383                        { 
     384                                // Make sure all fields are defined 
     385                                isset($this[$field]) or $this[$field] = NULL; 
     386                        } 
    337387                } 
    338388 
     
    374424                                        foreach ($all_fields as $f) 
    375425                                        { 
    376                                                 // Prevent other rules from running when this field already has errors 
    377                                                 if ( ! empty($this->errors[$f])) break; 
    378  
    379                                                 // Don't process rules on empty fields 
    380                                                 if ( ! in_array($func[1], $this->empty_rules, TRUE) AND $this[$f] == NULL) 
    381                                                         continue; 
    382  
    383                                                 // Run each rule 
    384                                                 if ( ! call_user_func($func, $this[$f], $args)) 
     426                                                if (isset($this->array_fields[$f])) 
    385427                                                { 
    386                                                         $this->errors[$f] = is_array($func) ? $func[1] : $func; 
     428                                                        // Use the field key 
     429                                                        $f_key = $this->array_fields[$f]; 
     430 
     431                                                        // Prevent other rules from running when this field already has errors 
     432                                                        if ( ! empty($this->errors[$f_key])) break; 
     433 
     434                                                        // Don't process rules on empty fields 
     435                                                        if ( ! in_array($func[1], $this->empty_rules, TRUE) AND $this[$f_key] == NULL) 
     436                                                                continue; 
     437 
     438                                                        foreach ($this[$f_key] as $k => $v) 
     439                                                        { 
     440                                                                if ( ! call_user_func($func, $this[$f_key][$k], $args)) 
     441                                                                { 
     442                                                                        // Run each rule 
     443                                                                        $this->errors[$f_key] = is_array($func) ? $func[1] : $func; 
     444                                                                } 
     445                                                        } 
     446                                                } 
     447                                                else 
     448                                                { 
     449                                                        // Prevent other rules from running when this field already has errors 
     450                                                        if ( ! empty($this->errors[$f])) break; 
     451 
     452                                                        // Don't process rules on empty fields 
     453                                                        if ( ! in_array($func[1], $this->empty_rules, TRUE) AND $this[$f] == NULL) 
     454                                                                continue; 
     455 
     456                                                        if ( ! call_user_func($func, $this[$f], $args)) 
     457                                                        { 
     458                                                                // Run each rule 
     459                                                                $this->errors[$f] = is_array($func) ? $func[1] : $func; 
     460                                                        } 
    387461                                                } 
    388462                                        } 
     
    390464                                else 
    391465                                { 
    392                                         // Prevent other rules from running when this field already has errors 
    393                                         if ( ! empty($this->errors[$field])) break; 
    394  
    395                                         // Don't process rules on empty fields 
    396                                         if ( ! in_array($func[1], $this->empty_rules, TRUE) AND $this[$field] == NULL) 
    397                                                 continue; 
    398  
    399                                         // Run each rule 
    400                                         if ( ! call_user_func($func, $this[$field], $args)) 
     466                                        if (isset($this->array_fields[$field])) 
    401467                                        { 
    402                                                 $this->errors[$field] = is_array($func) ? $func[1] : $func; 
    403                                                 // Stop after an error is found 
    404                                                 break; 
     468                                                // Use the field key 
     469                                                $field_key = $this->array_fields[$field]; 
     470 
     471                                                // Prevent other rules from running when this field already has errors 
     472                                                if ( ! empty($this->errors[$field_key])) break; 
     473 
     474                                                // Don't process rules on empty fields 
     475                                                if ( ! in_array($func[1], $this->empty_rules, TRUE) AND $this[$field_key] == NULL) 
     476                                                        continue; 
     477 
     478                                                foreach ($this[$field_key] as $k => $val) 
     479                                                { 
     480                                                        if ( ! call_user_func($func, $this[$field_key][$k], $args)) 
     481                                                        { 
     482                                                                // Run each rule 
     483                                                                $this->errors[$field_key] = is_array($func) ? $func[1] : $func; 
     484 
     485                                                                // Stop after an error is found 
     486                                                                break 2; 
     487                                                        } 
     488                                                } 
     489                                        } 
     490                                        else 
     491                                        { 
     492                                                // Prevent other rules from running when this field already has errors 
     493                                                if ( ! empty($this->errors[$field])) break; 
     494 
     495                                                // Don't process rules on empty fields 
     496                                                if ( ! in_array($func[1], $this->empty_rules, TRUE) AND $this[$field] == NULL) 
     497                                                        continue; 
     498 
     499                                                if ( ! call_user_func($func, $this[$field], $args)) 
     500                                                { 
     501                                                        // Run each rule 
     502                                                        $this->errors[$field] = is_array($func) ? $func[1] : $func; 
     503 
     504                                                        // Stop after an error is found 
     505                                                        break; 
     506                                                } 
    405507                                        } 
    406508                                } 
     
    442544                                        foreach ($all_fields as $f) 
    443545                                        { 
     546                                                if (isset($this->array_fields[$f])) 
     547                                                { 
     548                                                        // Use the field key 
     549                                                        $f = $this->array_fields[$f]; 
     550                                                } 
     551 
    444552                                                // Process each filter 
    445553                                                $this[$f] = is_array($this[$f]) ? array_map($func, $this[$f]) : call_user_func($func, $this[$f]); 
     
    448556                                else 
    449557                                { 
     558                                        if (isset($this->array_fields[$field])) 
     559                                        { 
     560                                                // Use the field key 
     561                                                $field = $this->array_fields[$field]; 
     562                                        } 
     563 
    450564                                        // Process each filter 
    451565                                        $this[$field] = is_array($this[$field]) ? array_map($func, $this[$field]) : call_user_func($func, $this[$field]);