CodeIgniter Forums

Full Version: Problem with set_select( ) function - CI Validation Class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

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:
<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 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 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>

    &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>

Controller File
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'] = "";

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

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

El Forum

[eluser]GSV Sleeper Service[/eluser]
would it not be easier to use the form_dropdown() helper?

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

El Forum

Multiple div's with the same `id` is not valid coding also. Wink

El Forum

You could use this to make it easier for yourself Wink

El Forum


Can you explain me how to use this helper please ?

thanks in advance,

El Forum

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.

El Forum

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:

050505050505050505<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
&lt;html xmlns=""&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;link type="text/css" rel="stylesheet" href=" CodeIgniter/css/style.css" /&gt;
&lt;title&gt;Registration page - Step3&lt;/title&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 ??

El Forum

Xwero has left some rogue debug code in there. Tongue

Delete this line in the form_dropdown method:
Check for others too.

El Forum

Thanks !!!

Using MY_form_helper has solved my problem !

El Forum

It happens to the best of us :coolsmile: Sorry
Pages: 1 2