title: Codeigniter - How to populate form from database?
body: <p>I have a small site which allows a user to enter values in a form and then either submit it directly or store the field values in a template to later submit it. To submit the form later, he can load the previously saved template. For that there are three buttons <strong>Load Template</strong> / <strong>Save Template</strong> / <strong>Submit form</strong>.</p>
  
  <p>Because i am using the form validation built-in functionality from Codeigniter i run into problems when i want to populate the form with a template, which had been previously stored.</p>
  
  <p>The form fields are all set up like</p>
  
  <pre><code>$name = array(
  'name'  =&gt; 'name',
  'id'    =&gt; 'name',
  'value' =&gt; set_value('name', $form_field_values['name'])
  );
  </code></pre>
  
  <p>The variable <code>$form_field_values</code> holds the values from either a loaded template in the case when a template has been loaded or the default values when the form is first loaded.</p>
  
  <p>Initially the form is loaded with the default values. When i click on <strong>Load Template</strong> the values from the template are not chosen by <code>set_value()</code> because there were the default values in there before. What i want is <strong>to replace the values of the form fields with the ones from the template</strong>.</p>
  
  <p>Do you have any idea how to do that in a clean approach? What i have done is to introduce a variable to skip the call to <code>set_value()</code> completely like:</p>
  
  <pre><code>$name= array(
  'name'  =&gt; 'name',
  'id'    =&gt; 'name',
  'value' =&gt; $skip_form_validation ? $form_field_values['name'] : set_value('name', $form_field_values['name'])
  );
  </code></pre>
  
  <p>Where <code>$skip_form_validation</code> is a variable set in the controller, based on what button was pressed. Form validation is skipped for saving/loading a template.</p>