WreckTangle
WreckTangle

Reputation: 71

Codeigniter Form Extend Helper

Im trying to do chained select using jquery chained, but codeigniter echo form dropdown does not allow individual assignment of CLASS.

I would like to assign CLASS to each list like the example below.

<select name="hardware">
<option class="printer" value="" selected="selected"></option>
<option class="printer" value="EPSON">EPSON</option>
<option class="printer" value="HP">HP</option>
<option class="hdd" value="WD">WD</option>
<option class="hdd" value="SEAGATE">SEAGATE</option>
</select>  

and here is the codeigniter form dropdown

VIEW PAGE:

 <form action="" method="">
    $select = 'hardware';
    echo form_dropdown('hardware', $hardware,set_value('hardware',$this->input->post('hardware'))); ?>  
 </form>  

I have changed the select form to this.

<select name="supplier">
<?php foreach($supplier as $row){ ?>
<option value="<?php echo $row['supplier'];?>"><?php echo $row['supplier'];?>  
</option>
<?php }?>
</select>  

how can i return the selected value after a failed validation?

Upvotes: 0

Views: 328

Answers (2)

sunny
sunny

Reputation: 1166

what you want to do, you will have to create your own helper,See "Extending Helpers" here in the docs. I would do what is says and copy a "MY_helper.php" version to your application folder; don't mess with the core unless you really have to.

http://ellislab.com/codeigniter/user-guide/general/helpers.html

You could use the values array and set a class and item in an array (and change the foreach near line 327 in the helper) or pass another array and check it in the foreach.

Upvotes: 0

Ananth
Ananth

Reputation: 1550

This is not possible using the form_dropdown() because codeigniter is not allowing any parameter to set the "extra" attributes like class in the option tag.

Check this function : this is the core function form_dropdown() :

function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
    {
        if ( ! is_array($selected))
        {
            $selected = array($selected);
        }

        // If no selected state was submitted we will attempt to set it automatically
        if (count($selected) === 0)
        {
            // If the form name appears in the $_POST array we have a winner!
            if (isset($_POST[$name]))
            {
                $selected = array($_POST[$name]);
            }
        }

        if ($extra != '') $extra = ' '.$extra;

        $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

        $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";

        foreach ($options as $key => $val)
        {
            $key = (string) $key;

            if (is_array($val) && ! empty($val))
            {
                $form .= '<optgroup label="'.$key.'">'."\n";

                foreach ($val as $optgroup_key => $optgroup_val)
                {
                    $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';

                    $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
                }

                $form .= '</optgroup>'."\n";
            }
            else
            {
                $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';

                $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
            }
        }

        $form .= '</select>';

        return $form;
    }

Upvotes: 0

Related Questions