Adding disabled option in codeigniter form_dropdown
Asked Answered
D

8

9

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

Davon answered 23/7, 2011 at 1:2 Comment(0)
P
1

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.

Physician answered 1/5, 2015 at 4:36 Comment(1)
Will not work in CI 3.1.9, as $ key will be escaped.Danseur
C
0

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.

Choirboy answered 23/7, 2011 at 9:39 Comment(0)
A
0

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');
Audition answered 1/11, 2015 at 21:42 Comment(0)
U
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');
Unwell answered 7/1, 2016 at 8:30 Comment(1)
This makes the <select> disabled and not just a particular <option> as the OP specified.Burnout
H
0
<?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

Hallo answered 26/7, 2018 at 10:50 Comment(1)
Using js should be a last resort. This answer is missing its educational explanation.Borries
B
0

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']);?>
Balakirev answered 19/9, 2022 at 8:41 Comment(0)
T
-2

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.

Threadfin answered 23/7, 2011 at 1:14 Comment(3)
Thanks for the reply. By adding 'disabled="disabled"' to the end, the whole select list becomes disabled. I just want the first option (value 0) to become disabled, so that you cannot choose the "Choose a category..." option. Yes, maybe I should type out the form myself then, unless there is a solution to this.Davon
Try option groups, however, they are not visible when the select is not open: w3schools.com/tags/tag_optgroup.aspThreadfin
I do the following: I don't give the first option a value '' and use the form validation library to make the select a required field.Threadfin
P
-2

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

Paid answered 7/7, 2017 at 1:22 Comment(1)
Rather than modify the core code, it could be better to inject html or use javascript on the front-end. $('select[name=yourname]').prepend('<option selected disabled="disabled">Your choice</option>');Euterpe

© 2022 - 2024 — McMap. All rights reserved.