Welcome Guest, Not a member yet? Register   Sign In
Form_Dropdown with Blank "Select One..." Option
#9

[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 = '')
{
if ( ! is_array($options))
{
  $options = array($options);
}

$options = array_merge(array(null => 'Select One...'), $options);

return form_dropdown($name, $options, $selected, $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.


Messages In This Thread
Form_Dropdown with Blank "Select One..." Option - by El Forum - 08-07-2012, 03:08 AM
Form_Dropdown with Blank "Select One..." Option - by El Forum - 08-07-2012, 04:19 AM
Form_Dropdown with Blank "Select One..." Option - by El Forum - 08-07-2012, 05:01 AM
Form_Dropdown with Blank "Select One..." Option - by El Forum - 08-07-2012, 05:07 AM
Form_Dropdown with Blank "Select One..." Option - by El Forum - 08-07-2012, 05:11 AM
Form_Dropdown with Blank "Select One..." Option - by El Forum - 08-07-2012, 05:12 AM
Form_Dropdown with Blank "Select One..." Option - by El Forum - 08-07-2012, 05:14 AM
Form_Dropdown with Blank "Select One..." Option - by El Forum - 08-07-2012, 05:27 AM
Form_Dropdown with Blank "Select One..." Option - by El Forum - 08-07-2012, 09:36 PM
Form_Dropdown with Blank "Select One..." Option - by El Forum - 08-08-2012, 03:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB