Changeset 1915
- Timestamp:
- 02/04/2008 07:00:18 PM (10 months ago)
- Location:
- trunk/modules/forge
- Files:
-
- 3 modified
-
controllers/forge_demo.php (modified) (1 diff)
-
libraries/Forge.php (modified) (2 diffs)
-
libraries/Form_Input.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/forge/controllers/forge_demo.php
r1658 r1915 38 38 39 39 echo $form->html(); 40 41 // Using a custom template: 42 // echo $form->html('custom_view', TRUE); 43 // Inside the view access the inputs using $input_id->html(), ->label() etc 44 // 45 // To get the errors use $input_id_errors. 46 // Set the error format with $form->error_format('<div>{message}</div>'); 47 // Defaults to <p class="error">{message}</p> 48 // 49 // Examples: 50 // echo $username->html(); echo $password_errors; 40 51 } 41 52 -
trunk/modules/forge/libraries/Forge.php
r1853 r1915 19 19 public $hidden = array(); 20 20 21 // Error message format, only used with custom templates 22 public $error_format = '<p class="error">{message}</p>'; 23 public $newline_char = "\n"; 24 21 25 public function __construct($action = '', $title = '', $method = NULL, $attr = array()) 22 26 { … … 120 124 } 121 125 126 public function error_format($string = '') 127 { 128 if (strpos((string) $string, '{message}') === FALSE) 129 throw new Kohana_Exception('validation.error_format'); 130 131 $this->error_format = $string; 132 } 133 122 134 /** 123 135 * Creates the form HTML 124 136 * 125 137 * @param string form view template name 138 * @param boolean use a custom view 126 139 * @return string 127 140 */ 128 public function html($template = 'forge_template' )141 public function html($template = 'forge_template', $custom = FALSE) 129 142 { 130 143 // Load template with current template vars 131 144 $form = new View($template, $this->template); 132 145 133 $hidden = array(); 134 if ( ! empty($this->hidden)) 135 { 136 foreach($this->hidden as $input) 137 { 138 $hidden[$input->name] = $input->value; 139 } 140 } 141 142 $form_type = 'open'; 143 // See if we need a multipart form 144 foreach ($this->inputs as $input) 145 { 146 if ($input instanceof Form_Upload) 147 { 148 $form_type = 'open_multipart'; 149 break; 150 } 151 } 152 153 // Set the form open and close 154 $form->open = form::$form_type(arr::remove('action', $this->attr), $this->attr, $hidden); 155 $form->close = form::close(); 156 157 // Set the inputs 158 $form->inputs = $this->inputs; 159 160 return $form->render(); 146 if ($custom) 147 { 148 // Using a custom view 149 150 $data = array(); 151 foreach ($this->inputs as $input) 152 { 153 $data[$input->name] = $input; 154 155 // Compile the error messages for this input 156 $messages = ''; 157 $errors = $input->error_messages(); 158 if (is_array($errors) AND ! empty($errors)) 159 { 160 foreach($errors as $error) 161 { 162 // Replace the message with the error in the html error string 163 $messages .= str_replace('{message}', $error, $this->error_format).$this->newline_char; 164 } 165 } 166 167 $data[$input->name.'_errors'] = $messages; 168 } 169 170 $form->set($data); 171 } 172 else 173 { 174 // Using a template view 175 176 $hidden = array(); 177 if ( ! empty($this->hidden)) 178 { 179 foreach($this->hidden as $input) 180 { 181 $hidden[$input->name] = $input->value; 182 } 183 } 184 185 $form_type = 'open'; 186 // See if we need a multipart form 187 foreach ($this->inputs as $input) 188 { 189 if ($input instanceof Form_Upload) 190 { 191 $form_type = 'open_multipart'; 192 } 193 } 194 195 // Set the form open and close 196 $form->open = form::$form_type(arr::remove('action', $this->attr), $this->attr, $hidden); 197 $form->close = form::close(); 198 199 // Set the inputs 200 $form->inputs = $this->inputs; 201 } 202 203 return $form; 161 204 } 162 205 -
trunk/modules/forge/libraries/Form_Input.php
r1888 r1915 248 248 else 249 249 { 250 // Strip 'valid_' from func name 251 $func = (substr($func, 0, 6) === 'valid_') ? substr($func, 6) : $func; 250 252 // Fetch an i18n error message 251 253 $error = Kohana::lang('validation.'.$func, $args);
