CodeIgniter Forums
form_dropdown() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: form_dropdown() (/showthread.php?tid=80457)



form_dropdown() - 68thorby68 - 11-03-2021

Hi all,
Does anyone know is there is a way to place additional attributes to the option tag in a Select dropdown, using the form_dropdown() form helper?
I am trying to achieve a out put like this
Code:
<select class="floating-select" name="tam_day_label" id="tam_day_label" onclick="this.setAttribute(\'value\', this.value);" value=" " required/>
    <option value="Monday" rel="1">Monday</option>
    <option value="Tuesday" rel="2">Tuesday</option>
    <option value="Wednesday" rel="3">Wednesday</option>
    <option value="Thursday" rel="4">Thursday</option>
    <option value="Friday" rel="5">Friday</option>
    <option value="Saturday" rel="6">Saturday</option>
    <option value="Sunday" rel="7">Sunday</option>
    <option value="Week" rel="8">Week</option>
</select>

my current form dropdown (select) is compiled like below:
Controller
PHP Code:
$dayArray=[
 
$value['monday']='Monday';
 
$value['tuesday']='Tuesday';
 
$value['wednesday']='Wednesday';
 
$value['thursday']='Thursday';
 
$value['friday']='Friday';
 
$value['saturday']='Saturday';
 
$value['sunday']='Sunday';
 ];

$data['dayType_tags'] = 'class="floating-select" id="tam_std_rate_type" onclick="this.setAttribute(\'value\', this.value);" value=" " required'
View
PHP Code:
<?= form_dropdown('tam_day_label'$dayArrayold('tam_day_label'), $dayType_tags); ?>

Output
Code:
<select class="floating-select" name="tam_day_label" id="tam_day_label" onclick="this.setAttribute(\'value\', this.value);" value=" " required/>
    <option value="Monday" rel="1">Monday</option>
    <option value="Tuesday" rel="2">Tuesday</option>
    <option value="Wednesday" rel="3">Wednesday</option>
    <option value="Thursday" rel="4">Thursday</option>
    <option value="Friday" rel="5">Friday</option>
    <option value="Saturday" rel="6">Saturday</option>
    <option value="Sunday" rel="7">Sunday</option>
    <option value="Week" rel="8">Week</option>
</select>

Many thanks.


RE: form_dropdown() - includebeer - 11-05-2021

The easiest way to know is looking at the code: https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Helpers/form_helper.php#L267
The short answer: it's not possible. 
Anyway, I'm not sure how it could be implemented since it takes a simple key/value array as input for the options.


RE: form_dropdown() - 68thorby68 - 11-09-2021

Thank you includebeer.

I toyed with this for quite a while, obviously with no luck and thought I was missing something. I will have to take a different approach, but isn't that half the fun?

Again, many thanks