![]() |
Form_Dropdown with Blank "Select One..." Option - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Form_Dropdown with Blank "Select One..." Option (/showthread.php?tid=53741) |
Form_Dropdown with Blank "Select One..." Option - El Forum - 08-07-2012 [eluser]dhall[/eluser] I have several forms on my site that have dropdowns, both static options and dynamic from a database table. I would like the default option to be "Select One..." with a blank key that would throw an error if an actual option is not selected when the form is submitted. Right now the first option in my dropdown is always shown. What can I do to have this done on all dropdowns? I figure modifying the actual form_dropdown helper, but I am not sure how. Thanks for the help. Form_Dropdown with Blank "Select One..." Option - El Forum - 08-07-2012 [eluser]ppwalks[/eluser] Post your view please Form_Dropdown with Blank "Select One..." Option - El Forum - 08-07-2012 [eluser]michalsn[/eluser] Validation: Code: $this->form_validation->set_rules('is_active', 'Is active', 'required'); Code: $is_active_dropdown = array( Code: $query = $this->db->get('is_active_dropdown'); Code: <?php echo form_dropdown('is_active', $is_active_dropdown, set_value('is_active')); ?> Form_Dropdown with Blank "Select One..." Option - El Forum - 08-07-2012 [eluser]dhall[/eluser] Here's an example. Controller: Code: $data['gender_options'] = $this->dropdown_model->get_genders(); Model: Code: function get_genders() View: Code: echo form_label('Gender: ').br(1); So in html this shows: Code: <label>Gender: </label> I want it to show: Code: <label>Gender: </label> And if they do not select one then it returns an error. Form_Dropdown with Blank "Select One..." Option - El Forum - 08-07-2012 [eluser]ppwalks[/eluser] The easiest way to do it is build the select manually, so as simple as this: Code: <select name="your_select" id=""> Form_Dropdown with Blank "Select One..." Option - El Forum - 08-07-2012 [eluser]ppwalks[/eluser] Then for validation just check if the value is empty... Form_Dropdown with Blank "Select One..." Option - El Forum - 08-07-2012 [eluser]ppwalks[/eluser] A couple of quotes where wrong so readjust to format Form_Dropdown with Blank "Select One..." Option - El Forum - 08-07-2012 [eluser]michalsn[/eluser] Code: $this->form_validation->set_rules('gender', 'Gender', 'required'); Code: function get_genders() Code: echo form_dropdown('gender', $gender_options, set_value('gender', $gender_answer)); Form_Dropdown with Blank "Select One..." Option - El Forum - 08-07-2012 [eluser]Aken[/eluser] While most of these situations work, if you want to do it to EVERY form dropdown, that's a lot of additional code. Keep things DRY - don't repeat yourself. To extend the form helper, start by creating a file at /application/helpers/MY_form_helper.php (or, if you use your own prefix instead of MY, use that). This file will automatically be included when loading the form validation library, or the form helper (validation library loads the form helper for you). Then you have one of two options: 1) Copy the original form_dropdown() and modify it to add the default first option. array_merge() is a good solution for that. Code: $options = array_merge(array(null => 'Select One...'), $options); PROS: Retain original function name, form_multiselect() still compatible. CONS: If function is updated in the future, need to update your code again. 2) Create your own function, that modifies the $options array before sending it to form_dropdown(). Code: function my_dropdown($name, $options = array(), $selected = array(), $extra = '') PROS: If form_dropdown() is updated, new functionality is retained (as long as the parameters stay the same). CONS: need to use your own function names, update existing code. You decide which is best for your situation. Form_Dropdown with Blank "Select One..." Option - El Forum - 08-08-2012 [eluser]dhall[/eluser] AWESOME... THANK YOU EVERYONE! You have given me several options (no pun intended) on how to setup my dropdowns. After looking at all my dropdowns I do have some where I would want the "Select One..." and others where I do not; so I think creating my own function including this will be my initial way to go. Thanks again. |