• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
form_dropdown helper --> disabled option

#1
Hello,

I can't mark specific options as disabled because no way to do it.

Code:
<option value="1" disabled> ddddd</option>


I tried to add it in options array by add the disabled attribute beside the value eg. :

PHP Code:
$options = array( 'small" disabled' => ''small shirt"); 


Also i tried :

PHP Code:
$options = array( 'small" disabled = "disabled' => ''small shirt"); 


But the problem with html_entities it converts the double quote to the entity : &quote


form_dropdown([$name = ''[$options = array()[$selected = array()[$extra = '']]]])

I'm looking forward to something like that:

form_dropdown([$name = ''[$options = array()[$selected = array()[$extra = '', $disabled_options = array()]]]]])

If there is an extra option called $disabled_options = array() it would be great.
Reply

#2
You can create your own MY_form_helper.php.
Put your form_dropdown() function inside this php file.
It will override the standard form_dropdown() function.
Reply

#3
Create custom dropdown function inside the application/helpers/MY_helper.php, and autoload your new helper.

PHP Code:
function my_form_dropdown($data ''$options = array(), $selected = array(), $disabled = array(), $hidden= array(), $extra '')
{
 
   $defaults = array();

 
   if (is_array($data))
 
   {
 
       if (isset($data['selected']))
 
       {
 
           $selected $data['selected'];
 
           unset($data['selected']); // select tags don't have a selected attribute
 
       }

 
       if (isset($data['options']))
 
       {
 
           $options $data['options'];
 
           unset($data['options']); // select tags don't use an options attribute
 
       }

 
       if (isset($data['disabled']))
 
       {
 
           $disabled $data['disabled'];
 
           unset($data['disabled']); // select tags don't use an disabled attribute
 
       }

 
       if (isset($data['hidden']))
 
       {
 
           $hidden $data['hidden'];
 
           unset($data['hidden']); // select tags don't use an hidden attribute
 
       }
 
   }
 
   else
    
{
 
       $defaults = array('name' => $data);
 
   }

 
   is_array($selected) OR $selected = array($selected);
 
   is_array($options) OR $options = array($options);
 
   is_array($disabled) OR $disabled = array($disabled);
 
   is_array($hidden) OR $hidden = array($hidden);

 
   // If no selected state was submitted we will attempt to set it automatically
 
   if (empty($selected))
 
   {
 
       if (is_array($data))
 
       {
 
           if (isset($data['name'], $_POST[$data['name']]))
 
           {
 
               $selected = array($_POST[$data['name']]);
 
           }
 
       }
 
       elseif (isset($_POST[$data]))
 
       {
 
           $selected = array($_POST[$data]);
 
       }
 
   }

 
   $extra _attributes_to_string($extra);

 
   $multiple = (count($selected) > && stripos($extra'multiple') === FALSE) ? ' multiple="multiple"' '';

 
   $form '<select '.rtrim(_parse_form_attributes($data$defaults)).$extra.$multiple.">\n";

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

 
       if (is_array($val))
 
       {
 
           if (empty($val))
 
           {
 
               continue;
 
           }

 
           $form .= '<optgroup label="'.$key."\">\n";

 
           foreach ($val as $optgroup_key => $optgroup_val)
 
           {
 
               $sel in_array($optgroup_key$selected) ? ' selected="selected"' '';
 
               $dis in_array($optgroup_key$disabled) ? ' disabled' '';
 
               $hid in_array($optgroup_key$hidden) ? ' hidden' '';
 
               $form .= '<option value="'.html_escape($optgroup_key).'"'.$sel.$dis.$hid.'>'
 
                   .(string) $optgroup_val."</option>\n";
 
           }

 
           $form .= "</optgroup>\n";
 
       }
 
       else
        
{
 
           $form .= '<option value="'.html_escape($key).'"'
 
               .(in_array($key$selected) ? ' selected="selected"' ''). (in_array($key$disabled) ? ' disabled'''). (in_array($key$hidden) ? ' hidden''').'>'
 
               .(string) $val."</option>\n";
 
       }
 
   }

 
   return $form."</select>\n";


It is the same as the core form_dropdown() function but with two new arguments ([array] disabled) , ([array] hidden):

Usage:
PHP Code:
$data = ['name' => 'countries', class='form-control'];
$options = [
 
'' => '-Select Country-',
 
'burundi' => 'Burundi',
 
'comoros' => 'Comoros',
 
'congo' => 'D.R Congo'
];
 echo 
my_form_dropdown($data$options''''''); 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.