CodeIgniter Forums
Problem with set_select( ) function - CI Validation Class - 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: Problem with set_select( ) function - CI Validation Class (/thread-8363.html)

Pages: 1 2


Problem with set_select( ) function - CI Validation Class - El Forum - 05-15-2008

[eluser]Salim[/eluser]
Hi all,

I have a persistent problem from many days ... and i have not found any answer in the forum.

I have the following form inside my view file. There is :
- 3 select
- 2 radio button
- 1 input text

All of them are passed to a validation process in the associated controller:

View File:
Code:
<form action="<?=base_url();?>index.php/registration/step3" method="post">
     &lt;?php if ($this->validation->day_error <> "") {?&gt;
    <div id="avertissement_red">&lt;?php echo $this->validation->day_error ?&gt;</div>
    &lt;?php } ?&gt;
    &lt;?php if ($this->validation->month_error <> "") {?&gt;
    <div id="avertissement_red">&lt;?php echo $this->validation->month_error ?&gt;</div>
    &lt;?php } ?&gt;
    &lt;?php if ($this->validation->year_error <> "") {?&gt;
    <div id="avertissement_red">&lt;?php echo $this->validation->year_error ?&gt;</div>
    &lt;?php } ?&gt;
    <p>Ta date de naissance :
    <select name="day" >
    <option value='Jour' &lt;?php $this->validation->set_select('day', 'Jour'); ?&gt; >Jour</option>
        <option value='31' &lt;?php $this->validation->set_select('day', '31'); ?&gt; >31</option>
        <option value='30' &lt;?php $this->validation->set_select('day', '30'); ?&gt; >30</option>
    </select>
    <select name="month">
    <option value='Mois' &lt;?php $this->validation->set_select('month', 'Mois'); ?&gt;>Mois</option>
        <option value='12' &lt;?php $this->validation->set_select('month', '12'); ?&gt; >12</option>
        <option value='11' &lt;?php $this->validation->set_select('month', '11'); ?&gt; >11</option>
        <option value='10' &lt;?php $this->validation->set_select('month', '10'); ?&gt; >10</option>
        <option value='09' &lt;?php $this->validation->set_select('month', '09'); ?&gt; >09</option>
        <option value='08' &lt;?php $this->validation->set_select('month', '08'); ?&gt; >08</option>
        <option value='07' &lt;?php $this->validation->set_select('month', '07'); ?&gt; >07</option>        
        <option value='06' &lt;?php $this->validation->set_select('month', '06'); ?&gt; >06</option>
        <option value='05' &lt;?php $this->validation->set_select('month', '05'); ?&gt; >05</option>
        <option value='04' &lt;?php $this->validation->set_select('month', '04'); ?&gt; >04</option>
        <option value='03' &lt;?php $this->validation->set_select('month', '03'); ?&gt; >03</option>
        <option value='02' &lt;?php $this->validation->set_select('month', '02'); ?&gt; >02</option>
        <option value='01' &lt;?php $this->validation->set_select('month', '01'); ?&gt; >01</option>
    </select>
    <select name="year">
        <option value='Annee' &lt;?php $this->validation->set_select('year', 'Annee'); ?&gt;>Ann&eacute;e</option>
        <option value='2000' &lt;?php $this->validation->set_select('year', '2000'); ?&gt;>2000</option>
        <option value='1999' &lt;?php $this->validation->set_select('year', '1999'); ?&gt;>1999</option>
        <option value='1998' &lt;?php $this->validation->set_select('year', '1998'); ?&gt;>1998</option>
        <option value='1997' &lt;?php $this->validation->set_select('year', '1997'); ?&gt;>1997</option>
        <option value='1996' &lt;?php $this->validation->set_select('year', '1996'); ?&gt;>1996</option>
        <option value='1995' &lt;?php $this->validation->set_select('year', '1995'); ?&gt;>1995</option>
    </select>

    </p>
    &lt;?php if ($this->validation->sexe_error <> "") {?&gt;
    <div id="avertissement_red">&lt;?php echo $this->validation->sexe_error ?&gt;</div>
&lt;?php } ?&gt;
    <p>Tu es : une femme &lt;input name="sexe" type="radio"  value="2" &lt;?php echo $this-&gt;validation->set_radio('sexe', '2'); ?&gt;/> ou un homme &lt;input name="sexe" type="radio"  value="1"  &lt;?php echo $this-&gt;validation->set_radio('sexe', '1'); ?&gt;/></p>
    <p>Télécharge ta photo : &lt;input name="photo" type="text"  value=""/&gt;</p>
    <p>&lt;input type="submit" value="Aller à l'&eacute;tape 4" /&gt;</p>
&lt;/form&gt;

Controller File
Code:
function step3 ()
    {
        $rules['day'] = "trim|callback_day_check";
        $rules['month'] = "trim";
        $rules['year'] = "trim";
        $rules['sexe'] = "trim|required|callback_sexe_check";
        
        $fields['day'] = 'Jour';
        $fields['month'] = 'Mois';
        $fields['year'] = 'Ann&eacute;e';
        $fields['sexe'] = 'Homme/Femme';

        $data['title'] = "Registration page - Step3";
        $data['heading'] = "";
        
        $this->validation->set_fields($fields);
        $this->validation->set_rules($rules);

        if ($this->validation->run() == FALSE)
        {
            $this->load->view('Registration/step3', $data);
        }
        else {    
                
            $this->step4();
        }                                    
    }

The validation process work fine, and all my rules are fully respected.

The probleme i have is that my select lists posted data are not saved if one of the others filled have an error during the validation.

For example, if i correctly choose a day / month / year, without choosing one of the 2 radio button, The error message for the missing radio button appears but all of my lists don't display the previous selected data...


Does anyone have a solution please ....


Problem with set_select( ) function - CI Validation Class - El Forum - 05-15-2008

[eluser]GSV Sleeper Service[/eluser]
would it not be easier to use the form_dropdown() helper? http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

I've not done this myself, so I'm not sure how easy this will be to integrate with the validation class.


Problem with set_select( ) function - CI Validation Class - El Forum - 05-15-2008

[eluser]wiredesignz[/eluser]
Multiple div's with the same `id` is not valid coding also. Wink


Problem with set_select( ) function - CI Validation Class - El Forum - 05-15-2008

[eluser]xwero[/eluser]
You could use this to make it easier for yourself Wink


Problem with set_select( ) function - CI Validation Class - El Forum - 05-15-2008

[eluser]Salim[/eluser]
xwero,

Can you explain me how to use this helper please ?

thanks in advance,


Problem with set_select( ) function - CI Validation Class - El Forum - 05-15-2008

[eluser]xwero[/eluser]
You name the file you put the code in MY_form_helper.php and put it in the /application/helpers directory. load the form helper as you used to and use the functions like documented in the user guide, there are two differences with the default functions you can see near the bottom of the wiki page.
But the major chance is that if you send the form the send values will be fetched and displayed for you without additional code.


Problem with set_select( ) function - CI Validation Class - El Forum - 05-15-2008

[eluser]Salim[/eluser]
Thanks xwero.

It works quite perfectly !!

The posted select data now appears after validation, but the selected data appears as well in the top of my view (html file) ...??

This is strange ...!??

Here is the code source of the page:

Code:
050505050505050505<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;link type="text/css" rel="stylesheet" href="http://127.0.0.1/Training CodeIgniter/css/style.css" /&gt;
&lt;title&gt;Registration page - Step3&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

I have selected the value '05' in my select list.
I don't put this variable in the beggining of my view file .. and it appears in the html code !!::;???

Do you know why I got this ??


Problem with set_select( ) function - CI Validation Class - El Forum - 05-15-2008

[eluser]wiredesignz[/eluser]
Xwero has left some rogue debug code in there. Tongue

Delete this line in the form_dropdown method:
Code:
print_r($send);
Check for others too.


Problem with set_select( ) function - CI Validation Class - El Forum - 05-15-2008

[eluser]Salim[/eluser]
Thanks !!!

Using MY_form_helper has solved my problem !


Problem with set_select( ) function - CI Validation Class - El Forum - 05-15-2008

[eluser]xwero[/eluser]
It happens to the best of us :coolsmile: Sorry