Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter: Controller structure for forms with many inputs
    text
    copied!<p>I'm developing a site using CodeIgniter and am trying to adhere to the "Fat Model / Skinny Controller" paradigm, but am running into some problems when it comes to pages containing forms with a number of inputs. The code below is what I'm using to define the inputs for the address fields </p> <p>Part of my Controller (where I'm defining the form inputs and their attributes):</p> <pre><code>$this-&gt;data['address1'] = array( 'name' =&gt; 'address1', 'id' =&gt; 'address1', 'type' =&gt; 'text', 'class' =&gt; 'field text addr', 'tabindex' =&gt; '10', 'value' =&gt; $this-&gt;form_validation-&gt;set_value('address1'), 'placeholder' =&gt; 'Street Address' ); $this-&gt;data['address2'] = array( 'name' =&gt; 'address2', 'id' =&gt; 'address2', 'type' =&gt; 'text', 'class' =&gt; 'field text addr', 'tabindex' =&gt; '11', 'value' =&gt; $this-&gt;form_validation-&gt;set_value('address2'), 'placeholder' =&gt; 'Address Line 2', ); $this-&gt;data['city'] = array( 'name' =&gt; 'city', 'id' =&gt; 'city', 'type' =&gt; 'text', 'class' =&gt; 'field text addr', 'tabindex' =&gt; '12', 'value' =&gt; $this-&gt;form_validation-&gt;set_value('city'), 'placeholder' =&gt; 'City' ); $this-&gt;data['state'] = array( 'name' =&gt; 'state', 'id' =&gt; 'state', 'class' =&gt; 'field addr', 'tabindex' =&gt; '13', 'value' =&gt; $this-&gt;form_validation-&gt;set_value('state'), 'label' =&gt; array('class' =&gt; 'desc') ); $this-&gt;data['zip'] = array( 'name' =&gt; 'zip', 'id' =&gt; 'zip', 'type' =&gt; 'text', 'class' =&gt; 'field text addr', 'tabindex' =&gt; '14', 'maxlength' =&gt; '20', 'value' =&gt; $this-&gt;form_validation-&gt;set_value('zip'), 'placeholder' =&gt; 'Zip / Postal Code' ); $this-&gt;data['country'] = array( 'name' =&gt; 'country', 'id' =&gt; 'country', 'class' =&gt; 'field addr', 'tabindex' =&gt; '15', 'value' =&gt; $this-&gt;form_validation-&gt;set_value('country') ); </code></pre> <p>Part of my View (minus all the HTML to position the form inputs):</p> <pre><code>&lt;?php echo form_open("address/add"); echo form_input($address1); echo form_input($address2); echo form_input($city); $options = array(); $options[''] = 'State / Province / Region'; foreach($province_options AS $prov) { $options[$prov-&gt;id] = $prov-&gt;province; } echo form_dropdown('state',$options,'',$state); echo form_input($zip); $options = array(); $options[''] = 'Country'; foreach($country_options AS $cnt) { $options[$cnt-&gt;id] = $cnt-&gt;country; } echo form_dropdown('country',$options,'',$country); echo form_submit('submit', 'Submit &amp; Continue'); echo form_close(); ?&gt; </code></pre> <p>I feel like my Controller is overly verbose, but I can't think of what the alternative would be for how to organize the information necessary to represent my form if I'm planning on using the Form Helper to generate the form inputs in my view. Is this the right way to be doing things, or is there a better approach?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload