• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SOLVED: Dynamically dropdown <select> form validation

#1
[eluser]atno[/eluser]
Hi,

I have a <select> that is created dynamically from a database and it must validate to check if any option is selected. The options are about 58 and only one can be selected. If a user selects something but forgot to enter his/her name on submit the select will be reset, he/she will type his/her name and now when submit is pressed again there will be a new error showing that no option is selected!
I read the user_guide but I don't get it how to form_validate a <select> with so many dynamically generated options. What I understand is that every set_select() but be entered manually.
Is there a way to do it and i have missed the big picture?

Here's the code

Code:
<select name="test" size="1">

          &lt;?php

             foreach ($tests->result() as $test) {

                echo "<option value=\"$test->id\">" . $test->name . '</option>'. "\n";

              }

            ?&gt;
</select>

Cheers,
atno

#2
[eluser]Armchair Samurai[/eluser]
There are a couple of ways to approach this.

First, you can do the foreach() approach and just call set_select on each loop:

Code:
<select name="test" size="1">
&lt;?php $result = $test->result();

foreach ($result AS $test):?&gt;
    <option value="&lt;?=$test->id;?&gt;" &lt;?=set_select('test', $test->id);?&gt;>&lt;?=$test->name;?&gt;</option>
&lt;?php endforeach;?&gt;
</select>

The other option would be to use form_dropdown() and call set_value(), but you'd need to put the array in a format which form_dropdown can understand:

Code:
&lt;?php $result = $test->result();

foreach ($result AS $test)
    $arr[$test->id] = $test->name;


echo form_dropdown('test', $arr, set_value('test'), 'size="1"');?&gt;

#3
[eluser]atno[/eluser]
Will any of these methods auto select the previously (before page update) selected option?

#4
[eluser]Rok Biderman[/eluser]
I know it's offtopic, but i actually see a dropdown in the title instead of select tag. Might be wise to fix it.

#5
[eluser]atno[/eluser]
[quote author="Coccodrillo" date="1304425312"]I know it's offtopic, but i actually see a dropdown in the title instead of select tag. Might be wise to fix it.[/quote]

What do you mean?

#6
[eluser]danmontgomery[/eluser]
All you need is an option up top with a blank value, and you can set the field as required. I prefer form_dropdown(), as well.

Code:
$options = array('' => 'Select One');
foreach($tests->result() as $test) {
    $options[$test->id] = $test->name;
}
echo form_dropdown('test', $options, set_value('test'));

// Controller

$this->form_validation->set_rules('test', 'Test Field', 'required');

#7
[eluser]atno[/eluser]
Both foreach() and form_dropdown() are working but form_dropdown() is clearer to read.
Thank you both Armchair Samurai, noctrum.



atno

#8
[eluser]Rok Biderman[/eluser]
[quote author="atno" date="1304441236"][quote author="Coccodrillo" date="1304425312"]I know it's offtopic, but i actually see a dropdown in the title instead of select tag. Might be wise to fix it.[/quote]

What do you mean?[/quote]

I mean, this happens to rss feeds, because select tags don't get escaped (screenie attached below).

I said it was O/T Smile

#9
[eluser]Unknown[/eluser]
Code:
<select name="test" size="1">

          &lt;?php

             foreach ($tests->result() as $test) {

                echo "<option value=\"$test->id\">" . $test->name . '</option>'. "\n";

              }

            ?&gt;
</select>

&lt;input type="text" value="" id="one"/&gt;

same one... But i need to display the selected value in 'text' Box???


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.