Show
Ignore:
Timestamp:
12/07/2007 12:50:49 PM (13 months ago)
Author:
Shadowhand
Message:

Changes to Form_Input:

  • matches() updated to add a rule, rather than doing it's validate inline
  • callback() added; callbacks are defined like this: function my_callback($input_object)
  • validate() updated to handle matches and callbacks
  • add_error($func, $value) added to add custom errors to inputs
Files:
1 modified

Legend:

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

    r1442 r1443  
    1717        protected $protect = array(); 
    1818 
    19         // Validtion rules and callbacks 
     19        // Validation rules, matches, and callbacks 
    2020        protected $rules = array(); 
     21        protected $matches = array(); 
    2122        protected $callbacks = array(); 
    2223 
     
    8182        public function matches($input) 
    8283        { 
    83                 if ($this->validate() AND $input->validate() AND $this->value != $input->value) 
    84                 { 
    85                         $this->is_valid = FALSE; 
    86  
    87                         $this->errors['matches'] = array($input->name, $this->name); 
    88                 } 
    89  
    90                 return $this->is_valid; 
     84                if ( ! in_array($input, $this->matches)) 
     85                { 
     86                        $this->matches[] = $input; 
     87                } 
     88 
     89                return $this; 
     90        } 
     91 
     92        public function callback($callback) 
     93        { 
     94                if ( ! in_array($callback, $this->callbacks)) 
     95                { 
     96                        $this->callbacks[] = $callback; 
     97                } 
     98 
     99                return $this; 
    91100        } 
    92101 
     
    99108                else 
    100109                { 
    101                         $this->label = ($val === TRUE) ? ucwords(str_replace('_', ' ', $this->name)) : $val; 
     110                        $this->label = ($val === TRUE) ? ucfirst($this->name) : $val; 
    102111                        return $this; 
    103112                } 
     
    157166        } 
    158167 
     168        public function add_error($key, $val) 
     169        { 
     170                if ( ! isset($this->errors[$key])) 
     171                { 
     172                        $this->errors[$key] = $val; 
     173                } 
     174 
     175                return $this; 
     176        } 
     177 
    159178        protected function error_message() 
    160179        { 
     
    165184                foreach($this->errors as $func => $args) 
    166185                { 
    167                         if ( ! is_array($args)) 
    168                         { 
    169                                 $args = array(); 
    170                         } 
    171  
    172                         array_unshift($args, isset($this->data['label'])  
    173                                 ? strtolower($this->data['label'])  
    174                                 : $this->data['name']); 
     186                        if (is_string($args)) 
     187                        { 
     188                                $error = $args; 
     189                        } 
     190                        else 
     191                        { 
     192                                // Force args to be an array 
     193                                $args = is_array($args) ? $args : array(); 
     194 
     195                                array_unshift($args, isset($this->data['label']) 
     196                                        ? strtolower($this->data['label']) 
     197                                        : $this->data['name']); 
     198 
     199                                // Fetch an i18n error message 
     200                                $error = Kohana::lang('validation.'.$func, $args); 
     201                        } 
    175202 
    176203                        // Make the error into HTML 
    177                         $message .= '<p class="error">'.Kohana::lang('validation.'.$func, $args).'</p>'; 
    178                 } 
     204                        $message .= '<p class="error">'.$error.'</p>'; 
     205                } 
     206 
    179207                return $message; 
    180208        } 
     
    205233 
    206234                // No rules to validate 
    207                 if (count($this->rules) == 0) 
     235                if (count($this->rules) == 0 AND count($this->matches) == 0 AND count($this->callbacks) == 0) 
    208236                        return $this->is_valid = TRUE; 
    209237 
     
    271299                } 
    272300 
     301                if ( ! empty($this->matches)) 
     302                { 
     303                        foreach($this->matches as $input) 
     304                        { 
     305                                if ($this->value != $input->value) 
     306                                { 
     307                                        // Field does not match 
     308                                        $this->errors['matches'] = array($input->name); 
     309                                        break; 
     310                                } 
     311                        } 
     312                } 
     313 
     314                if ( ! empty($this->callbacks)) 
     315                { 
     316                        foreach($this->callbacks as $callback) 
     317                        { 
     318                                call_user_func($callback, $this); 
     319 
     320                                // Stop when an error occurs 
     321                                if ( ! empty($this->errors)) 
     322                                        break; 
     323                        } 
     324                } 
     325 
    273326                // If there are errors, validation failed 
    274327                return $this->is_valid = empty($this->errors);