Changeset 1915

Show
Ignore:
Timestamp:
02/04/2008 07:00:18 PM (10 months ago)
Author:
PugFish
Message:

Added ability to use custom forms in Forge, rather than using a template which iterates over each input there is a variable for each input allowing more customisable forms.
Fixed valid_ rules not finding the i18n string.

Location:
trunk/modules/forge
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/forge/controllers/forge_demo.php

    r1658 r1915  
    3838 
    3939                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; 
    4051        } 
    4152 
  • trunk/modules/forge/libraries/Forge.php

    r1853 r1915  
    1919        public $hidden = array(); 
    2020 
     21        // Error message format, only used with custom templates 
     22        public $error_format = '<p class="error">{message}</p>'; 
     23        public $newline_char = "\n"; 
     24 
    2125        public function __construct($action = '', $title = '', $method = NULL, $attr = array()) 
    2226        { 
     
    120124        } 
    121125 
     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 
    122134        /** 
    123135         * Creates the form HTML 
    124136         * 
    125137         * @param   string   form view template name 
     138         * @param   boolean  use a custom view 
    126139         * @return  string 
    127140         */ 
    128         public function html($template = 'forge_template') 
     141        public function html($template = 'forge_template', $custom = FALSE) 
    129142        { 
    130143                // Load template with current template vars 
    131144                $form = new View($template, $this->template); 
    132145 
    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; 
    161204        } 
    162205 
  • trunk/modules/forge/libraries/Form_Input.php

    r1888 r1915  
    248248                                else 
    249249                                { 
     250                                        // Strip 'valid_' from func name 
     251                                        $func = (substr($func, 0, 6) === 'valid_') ? substr($func, 6) : $func; 
    250252                                        // Fetch an i18n error message 
    251253                                        $error = Kohana::lang('validation.'.$func, $args);