Welcome Guest, Not a member yet? Register   Sign In
Multiselect Dropdown validation [Solved- Me Stupid :) ]
#11

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

[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;
#13

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

[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
#15

[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/11...15/#584104
#16

[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>
#17

[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;
#18

[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.
#19

[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.
#20

[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.




Theme © iAndrew 2016 - Forum software by © MyBB