CodeIgniter Forums
Multiselect Dropdown validation [Solved- Me Stupid :) ] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Multiselect Dropdown validation [Solved- Me Stupid :) ] (/thread-19010.html)

Pages: 1 2 3


Multiselect Dropdown validation [Solved- Me Stupid :) ] - El Forum - 05-25-2009

[eluser]xpix[/eluser]
the invisible code is back Smile


Multiselect Dropdown validation [Solved- Me Stupid :) ] - El Forum - 05-25-2009

[eluser]tkyy[/eluser]
use this to build your dropdown list, instead of the ci validation (replace your variables)

Code:
<?php
$o=null; foreach($programs_list as $option)
{
    if($_POST['program']==$option) $sel=' selected="selected"'; else $sel=null;
    $out    .=    "<option value='$option'$sel>$option</option>\n";
}

echo $out;
?&gt;



Multiselect Dropdown validation [Solved- Me Stupid :) ] - El Forum - 05-25-2009

[eluser]TheFuzzy0ne[/eluser]
Not any more. I tried printing a coffee and the liquid blew my printer up.


Multiselect Dropdown validation [Solved- Me Stupid :) ] - El Forum - 05-25-2009

[eluser]tkyy[/eluser]
[quote author="TheFuzzy0ne" date="1243276283"]Not any more. I tried printing a coffee and the liquid blew my printer up.[/quote]

i could really use some java atm, been coding for 15 hrs


Multiselect Dropdown validation [Solved- Me Stupid :) ] - El Forum - 05-25-2009

[eluser]TheFuzzy0ne[/eluser]
It's at times like that we need a caffeine drip. Although you're welcome to make yourself a coffee - http://ellislab.com/forums/viewthread/114868/P15/#584104


Multiselect Dropdown validation [Solved- Me Stupid :) ] - El Forum - 05-25-2009

[eluser]xpix[/eluser]
@tkyy

The output should be the same.

here is the html

<select size="5" name="programs[]">
<option value="109">xxxx</option>
<option value="106">cccc</option>
<option value="110">vvvvvvv</option>
<option value="102">aaaaa</option>
<option value="108">wwwww</option>
<option value="111">yyyyy</option>
<option value="103">bbbb</option>
</select>


Multiselect Dropdown validation [Solved- Me Stupid :) ] - El Forum - 05-25-2009

[eluser]tkyy[/eluser]
[quote author="xpix" date="1243276573"]@tkyy

The output should be the same.

here is the html

<select size="5" name="programs[]">
<option value="109">xxxx</option>
<option value="106">cccc</option>
<option value="110">vvvvvvv</option>
<option value="102">aaaaa</option>
<option value="108">wwwww</option>
<option value="111">yyyyy</option>
<option value="103">bbbb</option>
</select>[/quote]

yewza diz

Code:
&lt;?php
if(isset($_POST['program'])) $select_me=$_POST['program'];
if(!is_numeric($_POST['program']) || $_POST['program']==null) $select_me='106';

$o=null;foreach($programs_list as $option)
{
    if($select_me==$option) $sel=' selected="selected"'; else $sel=null;
    $out    .=    "<option value='$option'$sel>$option</option>\n";
}

echo $out;
?&gt;



Multiselect Dropdown validation [Solved- Me Stupid :) ] - El Forum - 05-25-2009

[eluser]xpix[/eluser]
So you say I should do a custom validation function.

The problem is: is this a bug in CI? Am I the only one with this problem?



This is taken from the Form_validation.php

Code:
// Cycle through the rules for each field, match the
        // corresponding $_POST item and test for errors
        foreach ($this->_field_data as $field => $row)
        {


Not an expert but it seems that the rules are applied on the POST fields. If a post does not exist but a rule exists the rule has no meaning.


The only solution I see here is to check for the POST value and throw a custom error.


Multiselect Dropdown validation [Solved- Me Stupid :) ] - El Forum - 05-25-2009

[eluser]TheFuzzy0ne[/eluser]
The _field_data variable is set when you set a rule using $this->form_validation->set_rules(). Please see the source code for the set_rules() method to see what information it holds. I'm quite sure the rule will be executed so long as no other rules appear before it. Things like trim() and checking it's actually been set should be possible from within your custom method.

I'm starting to have second thoughts however, so I'm going to try it out shortly, and then get back to you.


Multiselect Dropdown validation [Solved- Me Stupid :) ] - El Forum - 05-25-2009

[eluser]TheFuzzy0ne[/eluser]
I'm starting to wonder if it's an error in your code that's not allowing the validation to run. I ran this test:

Code:
&lt;?php

class Test extends Controller
{
    
    function Test()
    {
        parent::Controller();
    }
    
    function index()
    {
        $this->load->library('form_validation');
        
        if ($this->input->post('submit'))
        {
            $this->form_validation->set_rules('select', '', 'required');
            if ($this->form_validation->run())
            {
                echo "sorted!<br />";
                die('<pre>'.print_r($this->input->post('select'), TRUE).'</pre>');
            }

            echo validation_errors();
            die('<pre>'.print_r($_POST, TRUE).'</pre>');

        }
        
        $this->output->set_output($this->_print_page());
    }
    
    function _print_page()
    {
        return <<<EOT
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Test&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;form action="test" method="post"&gt;
            <select name="select[]" multiple="multiple">
                <option value="1">Option 1</option>
                <option value="2">Option 2</option>
                <option value="3">Option 3</option>
                <option value="4">Option 4</option>
                <option value="5">Option 5</option>
            </select>
            &lt;input type="submit" name="submit" value="Submit" /&gt;
        &lt;/form&gt;
    &lt;/body&gt;
&lt;/html&gt;
EOT;
        
    }
}

/* End of file test.php */
/* Location: ./system/application/controllers/test.php */

When I submitted the form without any items selected, the form validation library told me that it was a required field, which it should. This confirms that the validation library performs the test even if it doesn't exist in the post array.