Welcome Guest, Not a member yet? Register   Sign In
Form validation - remembering values for <select>
#1

[eluser]Dauntless[/eluser]
Hi,

I'm using the form validation class and I have a select field in my form:

Code:
<select name="team[]" multiple="multiple" size="3">
            <option value='-1' &lt;?= $this->validation->set_select('team', '-1')?&gt;>None</option>
            &lt;?
            foreach ( $teams as $key=>$val ){
                   print "<option value='$key' " . $this->validation->set_select('team', "$key").">$val</option>\n";
            }            ?&gt;
            </select>

The problem is that the values aren't remembered. I tried using 'team[]', but that doesn't work either ...

I am however able to load the data in the controller and process it:
Code:
$rules['team'] = "callback_team_check";

// ...
function team_check($arg)
    {
        if($arg[0] == -1 && count($arg) > 1)
        {
            $this->validation->set_message('team_check', '...');
            return false;
        }
        
        if(count($arg) == 0)
        {
            $this->validation->set_message('team_check', '...');
            return false;
        }
        
        return true;
    }

How can I get the validation class to remember my selected values ?

All help is appreciated! Smile
#2

[eluser]Dready[/eluser]
After a review of the validation library you can't use set_select with multiple-values select... :-(
The set_select test is :

Code:
if ($_POST[$field] == $value)

So if you pass 'team' (as in your example) as the field argument it will test $_POST['team'] , which is an array. If you pass 'team[]' it will test $_POST['team[]'] , which is not defined.
#3

[eluser]drewbee[/eluser]
Hi Dauntless,

There is a new validation library being worked on in the SVN: 'form validation'.

It supports multi-dimensional arrays as well as re-selecting it.

This will be part of the 1.7 release, however you can pull it out of the svn as well for the time being. (do a full checkout, as several helpers are changed as well to support this new form validation library)
#4

[eluser]Dauntless[/eluser]
Thanks for your replies, Dready & Drewbee Smile.

I'll check out the SVN and see what's easier: Writing my own little hack or updating the helper libraries.

At least now I know it's not something I'm doing wrong... Smile
#5

[eluser]drewbee[/eluser]
I agree. The new library has some very nice features to go along with it. I personally am glad I just started using CI, now I don't have to go through and revert a ton of code! There is a thread entitled "Who wants to test the new validation class", or similiar. It has all of us picking and suggesting fixes as well as things that could be improved upon.

Edit:
Here we are: http://ellislab.com/forums/viewthread/89251/




Theme © iAndrew 2016 - Forum software by © MyBB