Welcome Guest, Not a member yet? Register   Sign In
set_select() makes all options selected, ignores third parameter
#1

[eluser]wdm*[/eluser]
This is how I'm using it in my form:

Code:
<select name="origin" id="origin" class="has-other-option required">
    <option value="">-</option>
    &lt;? foreach ($origin as $key => $value) : ?&gt;
        <option value="&lt;?= $key ?&gt;" &lt;?= set_select('origin', $key, FALSE) ?&gt; >&lt;?= $value ?&gt;</option>
    &lt;? endforeach; ?&gt;
    <option value="-1">Other</option>
</select>

And this is the html output:

Code:
<select id="origin" class="has-other-option" name="origin">
    <option value="">-</option>
    <option selected="selected" value="1">ORF</option>
    <option selected="selected" value="2">AE PrixArchiv</option>
    <option selected="selected" value="3">AE Festivalbüro</option>
    <option selected="selected" value="5">Rathaus</option>
    <option selected="selected" value="4">AE FutureLab</option>
    <option value="-1">Other</option>
</select>

The docs say:

Quote:The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).

Looking at the Validation class, it seems not to take a third parameter?

Code:
function set_select($field = '', $value = '')

Is this a bug, or am I doing something wrong?
#2

[eluser]Vince Stross[/eluser]
I've experienced similar trouble with this new class:

Code:
<select id="content" name="content">
  <option value="0">None (inactive)</option>

&lt;? foreach($pages as $page):?&gt;
  <option value="&lt;?=$page['id']?&gt;" &lt;?=set_select('content',$page['id'],(isset($data['content'])&&$data['content']==$page['id']))?&gt;>&lt;?=$page['title']?&gt;</option>
&lt;? endforeach;?&gt;
</select>

This actually produces the desired result:

Code:
<select id="content" name="content">
  <option value="0">None (inactive)</option>
  
  <option value="21" selected="selected">About Us</option>
  <option value="15">Companies</option>
  <option value="13">Contact Us</option>
  <option value="18">Employment</option>
  <option value="9">Home</option>
  <option value="26">Links</option>
</select>

my problem is related to the field not being set properly after POST. After the form is submitted and is NOT valid, the form reloads and the set_select() ignores the third argument as it should since it is a default (only to be used on the first load). However, it fails to set ANY value at all:

Code:
<select id="content" name="content">
  <option value="0">None (inactive)</option>
  
  <option value="21">About Us</option>
  <option value="15">Companies</option>
  <option value="13">Contact Us</option>
  <option value="18">Employment</option>
  <option value="9">Home</option>
  <option value="26">Links</option>
</select>

So I guess I am not having the SAME issue as you, but definitely an issue related to the set_select() function not working properly. The third argument is recognized just fine on the first load for me, and ignored on subsequent loads as it should be, but it doesn't appear to be holding the selected value through POSTs. You mentioned that the third argument is ignored for you even on the initial load, but is it preserving the POST value and reloading the form properly?
#3

[eluser]Vince Stross[/eluser]
Correction: After playing with this, I have to report that the set_select() function is working properly for me. The issue (now resolved but not comfortable with) is that the form fields have to be defined whether they have rules or not. I was assuming that the set_value(), set_select(), etc. functions were doing this for me.

In other words:
Code:
$config=array(
    array('field'=>'display','label'=>'Display','rules'=>'trim|required|max_length[50]'),
    array('field'=>'content','label'=>'Content','rules'=>''),
);
$this->form_validation->set_rules($config);

I would much rather have this be something like:

Code:
$config=array(
  array('field'=>'display','label'=>'Display','rules'=>'trim|required|max_length[50]'),
  array('field'=>'content','label'=>'Content'),

     - or (at the VERY least) -

  array('field'=>'content','label'=>'Content','rules'=>false),
);
$this->form_validation->set_rules($config);

Something doesn't feel right about having to define the rule as '' when it would be cleaner to just omit it all-together and check in the CI code using isset().
#4

[eluser]wdm*[/eluser]
Everything seems to be working OK now I upgraded to 1.7, and updated my code re. the new Validation docs.
#5

[eluser]quasiperfect[/eluser]
i have kind of the same problem with 1.7 download or svn

let's say u have
Code:
<select id="sex" name="sex">
<option value="" &lt;?php echo set_select('sex', '', TRUE);?&gt;>select sex</option>
<option value="0" &lt;?php echo set_select('sex', '0');?&gt;>male</option>
<option value="1" &lt;?php echo set_select('sex', '1');?&gt;>female</option>
</select>

if there is no submit the 'selected="selected"' is added to every option
if i modify set_select function in the form helper from
Code:
if (count($_POST) === 0)
{
    return ' selected="selected"';
}
to
Code:
if (count($_POST) === 0)
{
    if ($default)
    {
        return ' selected="selected"';
    }
    else
    {
        return '';
    }
}
it works great or maybe my needs are different i don't know
#6

[eluser]hsl[/eluser]
I still have the same problem, does anyone have a solution?
#7

[eluser]Zelaza[/eluser]
I think I finally figured this out (after quite a while of playing with it).

It seems that, if you are using the old CI "validation" library in conjunction with the set_select() function embedded into your form html, there's a bug that causes every select option to be selected (selected='selected'), even if you are using the third parameter to set_function() correctly.

If you go to the newer CI "form_validation" library, the problem goes away.

If you have to stick with the "validation" library for some reason, you can modify the validation library's set_select() code as described by quasiperfect, or you can work around the problem by using the form_dropdown() helper function in conjunction with set_value() in your view code as shown below:

&lt;?php
$options = array('0' => 'Mortal', '2' => 'Demi-God', '4' => 'God');
echo form_dropdown('userlevel', $options, set_value('userlevel'));
?&gt;
#8

[eluser]solepixel[/eluser]
I'm using the latest version of CI (1.7.1) and this is my code:
Code:
<select name="category" id="category">
    &lt;?php foreach($categories->result() as $category){
        $default = ($defaults->category == $category->id) ? true : false; ?&gt;
        <option value="&lt;?php echo $category->id; ?&gt;" &lt;?php echo set_select('category', $category->id, $default); ?&gt;>&lt;?php echo $category->category; ?&gt; &nbsp;</option>
    &lt;?php } // end foreach ?&gt;
</select>&lt;!-- end #category --&gt;

I have echoed out all my variables in comments, and it's clearly not working correctly. Here's the output:
[ID]: [set_select()] - [defaults->category] - [default]
Code:
<select name="category" id="category">&lt;!--
    1:  selected="selected" - 1 - 1 --&gt;
    <option value="1" selected="selected">Scopes &nbsp;</option>&lt;!--
    2:  selected="selected" - 1 - --&gt;
    <option value="2" selected="selected">Binoculars &nbsp;</option>
</select>

Any ideas on this?
#9

[eluser]Unknown[/eluser]
This is the worst coded module ive rewritten about 50% of it any who:

Code:
/**
     * Set Select
     *
     * Enables pull-down lists to be set to the value the user
     * selected in the event of an error
     *
     * @access    public
     * @param    string
     * @param    string
     * @return    string
     */    
    function set_select($field = '', $value = '', $default = FALSE)
    {        
        if($_POST[$field] == $value) {
            
            return ' selected="selected"';
        }
    }
#10

[eluser]LiorBroshi[/eluser]
it should actually be:

Code:
if(isset($_POST[$field]) && $_POST[$field] == $value) {
            
            return ' selected="selected"';
        }




Theme © iAndrew 2016 - Forum software by © MyBB