CodeIgniter Forums
Form Validation Repopulation with Form Helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Form Validation Repopulation with Form Helper (/showthread.php?tid=15105)



Form Validation Repopulation with Form Helper - El Forum - 01-26-2009

[eluser]Dan Bowling[/eluser]
I'm having a little trouble finding the proper syntax for repopulating a form after it fails the form_validation class. The example on the User Guide doesn't use the form_helper.

How should I repopulate form data with check boxes, drop downs, and radio buttons that use the form_helper?

Any generic example would help.


Form Validation Repopulation with Form Helper - El Forum - 01-26-2009

[eluser]xzela[/eluser]
I had a hard time with that issue too. I'm not sure if this is the 'proper' way to do it, but i find this works pretty well.

HTML code:
Code:
<input name='text_box' value='<?php echo set_value('text_box'); ?>' />

Like i said, not sure if that's the proper way to do it.

Remember, you'll have to load the 'form' helper to use it.
For more details see:
http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html


Form Validation Repopulation with Form Helper - El Forum - 01-26-2009

[eluser]xzela[/eluser]
oops, I should have read your post more carefully:
sorry...


Here's what I do for select fields:

Code:
<select name='select_id'>
  <option value='' ></option>
  &lt;?php foreach($select_items as $item):?&gt;
    &lt;?php if(set_value('select_id') == $item['select_id']):?&gt;
      <option value='&lt;?php echo $item['select_id']; ?&gt;' selected > &lt;?php echo $item['text']; ?&gt;</option>
    &lt;?php else: ?&gt;
      <option value='&lt;?php echo $item['select_id']; ?&gt;' >&lt;?php echo $item['text']; ?&gt;</option>
   &lt;?php endif;?&gt;
  &lt;?php endforeach;?&gt;
</select>

Like many things involving code, there is probably a better way. This is just one example of how you could do it.


Form Validation Repopulation with Form Helper - El Forum - 01-27-2009

[eluser]Dan Bowling[/eluser]
Thanks for the suggestion.

I've been able to do some similar stuff with my own methods for this, but what I am really looking for is some method that works with the Form Helper.

I was able to get the drop down to work alright

Code:
&lt;?php
$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

echo form_dropdown('shirts', $options, set_value('shirts'));

?&gt;

But I'm stuck with this for checkboxes and radio buttons:

Code:
<h5>Check Boxes</h5>
Red: &lt;input type="checkbox" name="checkbox[]" value="red" &lt;?=set_checkbox('checkbox[]', 'red'); ?&gt;/&gt;
Blue: &lt;input type="checkbox" name="checkbox[]" value="blue" &lt;?=set_checkbox('checkbox[]', 'blue'); ?&gt;/&gt;
Green: &lt;input type="checkbox" name="checkbox[]" value="green" &lt;?=set_checkbox('checkbox[]', 'green'); ?&gt;/&gt;

<h5>Radio</h5>
Radio 1: &lt;input type="radio" name="myradio" value="1" &lt;?php echo set_radio('myradio', '1', TRUE); ?&gt; /&gt;
Radio 2: &lt;input type="radio" name="myradio" value="2" &lt;?php echo set_radio('myradio', '2'); ?&gt; /&gt;

The difference in elegance is astonishing. I guess it's more a matter of keeping my code clean, and consistent (in regards to using the form helper or not.)


Form Validation Repopulation with Form Helper - El Forum - 01-27-2009

[eluser]The Wizard[/eluser]
Hello,

please check this link
6 Minutes Tutorial


Form Validation Repopulation with Form Helper - El Forum - 01-30-2009

[eluser]phazei[/eluser]
I was thinking about using the form helper with the form validator as I was reading through the user guide just now and wondering about the same question.

Has anyone actually integrated the set_XXXX() functions into the form helper form_XXXX() functions?

If so, please share! :-)

If not, I'll eventually do that myself. Then I'd never have to worry about form population again. Tongue