Changeset 1548
- Timestamp:
- 12/16/07 13:12:42 (7 months ago)
- Files:
-
- trunk/modules/forge/libraries/Forge.php (modified) (1 diff)
- trunk/modules/forge/libraries/Form_Group.php (modified) (2 diffs)
- trunk/modules/forge/libraries/Form_Input.php (modified) (10 diffs)
- trunk/modules/forge/views/forge_template.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/modules/forge/libraries/Forge.php
r1543 r1548 23 23 $this->template['action'] = $action; 24 24 $this->template['title'] = $title; 25 $this->template['class'] = $class; 25 26 } 26 27 trunk/modules/forge/libraries/Form_Group.php
r1544 r1548 7 7 'type' => 'group', 8 8 'class' => 'group', 9 'label' => '' 9 'label' => '', 10 'message' => '' 10 11 ); 11 12 … … 40 41 } 41 42 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 42 56 public function html() 43 57 { trunk/modules/forge/libraries/Form_Input.php
r1522 r1548 9 9 protected $data = array 10 10 ( 11 'type' => 'text',12 'class' => 'textbox',13 'value' => '',11 'type' => 'text', 12 'class' => 'textbox', 13 'value' => '' 14 14 ); 15 15 … … 27 27 // Errors 28 28 protected $errors = array(); 29 protected $error_messages; 29 30 30 31 public function __construct($name) … … 43 44 if ($method == 'rules') 44 45 { 46 if (empty($args)) 47 return $this->rules; 48 45 49 // Set rules and action 46 50 $rules = $args[0]; … … 104 108 if ($val === NULL) 105 109 { 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; 110 115 } 111 116 else … … 116 121 } 117 122 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 118 137 public function html() 119 138 { … … 121 140 $this->validate(); 122 141 123 return $this->html_element() .$this->error_message();142 return $this->html_element(); 124 143 } 125 144 … … 129 148 130 149 unset($data['label']); 150 unset($data['message']); 131 151 132 152 return form::input($data); … … 179 199 } 180 200 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 183 227 // Make sure validation runs 184 228 is_null($this->is_valid) and $this->validate(); 185 229 186 $message = ''; 230 // Return single error 231 if ( ! is_array($this->error_messages)) 232 return array($this->error_messages); 233 234 $messages = array(); 187 235 foreach($this->errors as $func => $args) 188 236 { … … 199 247 array_unshift($args, $this->label ? strtolower($this->label) : $this->name); 200 248 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; 210 266 } 211 267 … … 215 271 return; 216 272 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); 222 275 223 276 if (is_string($this->data['value'])) trunk/modules/forge/views/forge_template.php
r1543 r1548 6 6 7 7 $sub_inputs = array(); 8 if ($input->type == 'group') 9 { 8 if ($input->type == 'group'): 10 9 $sub_inputs = $input->inputs; 11 10 … … 14 13 <th colspan="2"><?php echo $input->label() ?></th> 15 14 </tr> 15 <tr> 16 <td colspan="2"><p class="group_message"><?php echo $input->message() ?></p></td> 17 </tr> 16 18 <?php 17 19 18 } 19 else 20 { 20 else: 21 21 $sub_inputs = array($input); 22 } 22 endif; 23 23 24 24 foreach($sub_inputs as $input): … … 27 27 <tr> 28 28 <th><?php echo $input->label() ?></th> 29 <td><?php echo $input->html() ?></td> 29 <td> 30 <?php 31 32 echo $input->html(); 33 34 if ($message = $input->message()): 35 36 ?> 37 <p class="message"><?php echo $message ?></p> 38 <?php 39 40 endif; 41 42 foreach ($input->error_messages() as $error): 43 44 ?> 45 <p class="error"><?php echo $error ?></p> 46 <?php 47 48 endforeach; 49 50 ?> 51 </td> 30 52 </tr> 31 53 <?php
