Changeset 1548

Show
Ignore:
Timestamp:
12/16/07 13:12:42 (7 months ago)
Author:
PugFish
Message:

Forge updates:

  • Fixed class not being set
  • Added message() for inputs and groups
  • Added error_messages() for setting custom rule error messages
  • Moved error message HTML from library to template
  • Label HTML is not outputted if no label is set
  • Input rules can be fetched via rules() now
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/modules/forge/libraries/Forge.php

    r1543 r1548  
    2323                $this->template['action'] = $action; 
    2424                $this->template['title']  = $title; 
     25                $this->template['class']  = $class; 
    2526        } 
    2627 
  • trunk/modules/forge/libraries/Form_Group.php

    r1544 r1548  
    77                'type'  => 'group', 
    88                'class' => 'group', 
    9                 'label' => '' 
     9                'label' => '', 
     10                'message' => '' 
    1011        ); 
    1112 
     
    4041        } 
    4142 
     43        public function message($val = NULL) 
     44        { 
     45                if ($val === NULL) 
     46                { 
     47                        return $this->data['message']; 
     48                } 
     49                else 
     50                { 
     51                        $this->data['message'] = $val; 
     52                        return $this; 
     53                } 
     54        } 
     55 
    4256        public function html() 
    4357        { 
  • trunk/modules/forge/libraries/Form_Input.php

    r1522 r1548  
    99        protected $data = array 
    1010        ( 
    11                 'type'  => 'text', 
    12                 'class' => 'textbox', 
    13                 'value' => '', 
     11                'type'    => 'text', 
     12                'class'   => 'textbox', 
     13                'value'   => '' 
    1414        ); 
    1515 
     
    2727        // Errors 
    2828        protected $errors = array(); 
     29        protected $error_messages; 
    2930 
    3031        public function __construct($name) 
     
    4344                if ($method == 'rules') 
    4445                { 
     46                        if (empty($args)) 
     47                                return $this->rules; 
     48 
    4549                        // Set rules and action 
    4650                        $rules  = $args[0]; 
     
    104108                if ($val === NULL) 
    105109                { 
    106                         if ($name = $this->name) 
    107                         { 
    108                                 return form::label($name, $this->label); 
    109                         } 
     110                        if (isset($this->data['name']) AND isset($this->data['label'])) 
     111                        { 
     112                                return form::label($this->data['name'], $this->data['label']); 
     113                        } 
     114                        return FALSE; 
    110115                } 
    111116                else 
     
    116121        } 
    117122 
     123        public function message($val = NULL) 
     124        { 
     125                if ($val === NULL) 
     126                { 
     127                        if (isset($this->data['message'])) 
     128                                return $this->data['message']; 
     129                } 
     130                else 
     131                { 
     132                        $this->data['message'] = $val; 
     133                        return $this; 
     134                } 
     135        } 
     136 
    118137        public function html() 
    119138        { 
     
    121140                $this->validate(); 
    122141 
    123                 return $this->html_element().$this->error_message()
     142                return $this->html_element()
    124143        } 
    125144 
     
    129148 
    130149                unset($data['label']); 
     150                unset($data['message']); 
    131151 
    132152                return form::input($data); 
     
    179199        } 
    180200 
    181         protected function error_message() 
    182         { 
     201        public function error_messages($func = NULL, $message = NULL) 
     202        { 
     203                // Set custom error messages 
     204                if ( ! empty($func)) 
     205                { 
     206                        if (is_array($func)) 
     207                        { 
     208                                // Replace all 
     209                                $this->error_messages = $func; 
     210                        } 
     211                        else 
     212                        { 
     213                                if (empty($message)) 
     214                                { 
     215                                        // Single error, replaces all others 
     216                                        $this->error_messages = $func; 
     217                                } 
     218                                else 
     219                                { 
     220                                        // Add custom error 
     221                                        $this->error_messages[$func] = $message; 
     222                                } 
     223                        } 
     224                        return $this; 
     225                } 
     226 
    183227                // Make sure validation runs 
    184228                is_null($this->is_valid) and $this->validate(); 
    185229 
    186                 $message = ''; 
     230                // Return single error 
     231                if ( ! is_array($this->error_messages)) 
     232                        return array($this->error_messages); 
     233 
     234                $messages = array(); 
    187235                foreach($this->errors as $func => $args) 
    188236                { 
     
    199247                                array_unshift($args, $this->label ? strtolower($this->label) : $this->name); 
    200248 
    201                                 // Fetch an i18n error message 
    202                                 $error = Kohana::lang('validation.'.$func, $args); 
    203                         } 
    204  
    205                         // Make the error into HTML 
    206                         $message .= '<p class="error">'.$error.'</p>'; 
    207                 } 
    208  
    209                 return $message; 
     249                                if (isset($this->error_messages[$func])) 
     250                                { 
     251                                        // Use custom error message 
     252                                        $error = vsprintf($this->error_messages[$func], $args); 
     253                                } 
     254                                else 
     255                                { 
     256                                        // Fetch an i18n error message 
     257                                        $error = Kohana::lang('validation.'.$func, $args); 
     258                                } 
     259                        } 
     260 
     261                        // Add error to list 
     262                        $messages[] = $error; 
     263                } 
     264 
     265                return $messages; 
    210266        } 
    211267 
     
    215271                        return; 
    216272 
    217                 if ($value = self::$input->post($this->name)) 
    218                 { 
    219                         // Load POSTed value 
    220                         $this->data['value'] = $value; 
    221                 } 
     273                // Load POSTed value 
     274                $this->data['value'] = self::$input->post($this->name); 
    222275 
    223276                if (is_string($this->data['value'])) 
  • trunk/modules/forge/views/forge_template.php

    r1543 r1548  
    66 
    77$sub_inputs = array(); 
    8 if ($input->type == 'group') 
    9 
     8if ($input->type == 'group'): 
    109        $sub_inputs = $input->inputs; 
    1110 
     
    1413<th colspan="2"><?php echo $input->label() ?></th> 
    1514</tr> 
     15<tr> 
     16<td colspan="2"><p class="group_message"><?php echo $input->message() ?></p></td> 
     17</tr> 
    1618<?php 
    1719 
    18 
    19 else 
    20 
     20else: 
    2121        $sub_inputs = array($input);     
    22 
     22endif; 
    2323 
    2424foreach($sub_inputs as $input): 
     
    2727<tr> 
    2828<th><?php echo $input->label() ?></th> 
    29 <td><?php echo $input->html() ?></td> 
     29<td> 
     30<?php 
     31 
     32echo $input->html(); 
     33 
     34if ($message = $input->message()): 
     35 
     36?> 
     37<p class="message"><?php echo $message ?></p> 
     38<?php 
     39 
     40endif; 
     41 
     42foreach ($input->error_messages() as $error): 
     43 
     44?> 
     45<p class="error"><?php echo $error ?></p> 
     46<?php 
     47 
     48endforeach; 
     49 
     50?> 
     51</td> 
    3052</tr> 
    3153<?php