Adding disabled option in codeigniter form_dropdown
I'm trying to figure out how to add a disabled option in my dropdown, using codeIgniter. New to CI, and I've tried googling it a bit, but couldn't find 开发者_开发百科an answer.
My code for a dropdown looks like this:
echo form_dropdown('category', array('0' => 'Choose a category...') + $categories, '0');
This gives me a dropdown with all my options from the variable $categories, with "Choose a category..." (value 0) at top. Now how to I make the first one disabled? I know how to make it select a specific one, which I've set it to do here.
Can anyone help me? Thanks
I know this is an old post, but in the current version of CI, I can do a little (sql-injection-like) trick by appending " disabled="disabled
to the keys of the option I would like to disable.
$categories['0'] = '(Select Category)';
$categories['1'] = 'Category 1';
$categories['2" disabled="disabled'] = 'Restricted Category';
$categories['3'] = 'Category 3';
echo form_dropdown('category', $categories, '0');
I am not sure if this is a bug of CI's form_helper, since it does not do any escaping or sanitizing function for the dropdown key/values. For the mean time, to be safe, just make sure your keys and values wont be coming from any user-based input.
If you still want to use form_helper, you always can extends helper file and make 'disable' tag available. Create MY_Form_helper.php and put that under helper directory, then define function form_dropdown in that custom helper, then it will overide the form helper behaviour.
If you have a few static options only the $options variable can be a simple string too, containing the options in html format, like this:
$options = "
<option value=0 disabled>Select Category</option>
<option value=1>Category 1</option>
<option value=2>Category 2</option>";
echo form_dropdown('category', $options, '0');
Just add the fourth param $extra to your dropdown as a string like this 'disabled=disabled' as explained in CI Docs at https://codeigniter.com/user_guide/helpers/form_helper.html#available-functions
echo form_dropdown('category', array('0' => 'Choose a category...') + $categories, '0', 'disabled=disabled');
<?php echo validation_errors(); ?>
<?php echo form_open(''); ?>
<?php echo form_label('Gender:'); ?>
<?php echo form_dropdown(array('id'=>'selectinform', 'name'=>'gender', 'options'=>array('1'=>'Select', '2'=>'2','3'=>'3'), 'selected'=>'1')); ?>
<?php echo form_submit(array('id'=>'submit', 'value'=>'submit')); ?>
<?php echo form_close(); ?>
<script>
$(document).ready(function(){
$("#selectinform option:first").attr('disabled', 'disabled');
});
</script>
Result - IMG
This works because codeigniter pass the key as full string:
<?php
$field['select'] = array(
'blah" disabled="disabled"' => 'Select some',
'0' => 'Never',
'1' => 'Daily',
'7' => 'Weekly',
'14' => 'Every 14 days',
'30' => 'Monthly',
);
?>
<?php echo form_dropdown($field['name'], $field['select'], (!empty($field['value'])) ? $field['value'] : 'blah" disabled="disabled"', 'id="' . $field['name'] . '" class="form-control"' . $field['extra']);?>
From the CI user guide at http://codeigniter.com/user_guide/helpers/form_helper.html:
If you would like the opening to contain additional data, like an id attribute or JavaScript, you can pass it as a string in the fourth parameter:
So you code becomes:
echo form_dropdown('category', array('0' => 'Choose a category...') + $categories, '0', 'disabled="disabled"');
However, unless you are a PHP fanatic or are using CSRF protection through the form helper I would just type out your form html yourself. It is the same amount of text and you are using less functions.
Solution:
Change the 437th string of form_helper.php
:
from
.(in_array($key, $selected) ? ' selected="selected"' : '').’>'
to
.(in_array($key, $selected) ? ' disabled="disabled"' : '').’>'
and use selected
feature as disabled
. Looks like an lifehack, but it works
精彩评论