• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Form_Dropdown with Blank "Select One..." Option

#1
[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.

#2
[eluser]ppwalks[/eluser]
Post your view please

#3
[eluser]michalsn[/eluser]
Validation:
Code:
$this->form_validation->set_rules('is_active', 'Is active', 'required');
Static array sample:
Code:
$is_active_dropdown = array(
    NULL => 'Select one...',
    0    => 'No',
    1    => 'Yes'
);
Dynamic array sample:
Code:
$query = $this->db->get('is_active_dropdown');
$data[0] = 'Select one...';
foreach ($query->result_array() as $row)
{
    $data[$row['id']] = $row['name'];
}
return $data;
View:
Code:
<?php echo form_dropdown('is_active', $is_active_dropdown, set_value('is_active')); ?>

#4
[eluser]dhall[/eluser]
Here's an example.

Controller:

Code:
$data['gender_options'] = $this->dropdown_model->get_genders();

Model:

Code:
function get_genders()
  {
    $options = array (
      'male' => 'Male',
      'female' => 'Female'
    );
    return $options;
  }

View:

Code:
echo form_label('Gender: ').br(1);
echo form_dropdown('gender',$gender_options,$gender_answer);

So in html this shows:
Code:
<label>Gender: </label>
<br>
<select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>

I want it to show:
Code:
<label>Gender: </label>
<br>
<select name="gender">
<option selected="selected" value="">Select One...</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>

And if they do not select one then it returns an error.

#5
[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="">
<option value="">Please Select</option>
&lt;?php foreach($youvar as $var) {
    echo "<option value='".$var['your_id_value_from_database']."'>'".$var['select_name']."'></option>";
}
?&gt;
</select>
Does that make sense?

#6
[eluser]ppwalks[/eluser]
Then for validation just check if the value is empty...

#7
[eluser]ppwalks[/eluser]
A couple of quotes where wrong so readjust to format

#8
[eluser]michalsn[/eluser]
Code:
$this->form_validation->set_rules('gender', 'Gender', 'required');
Code:
function get_genders()
{
    return array(
      NULL => 'Select One...',
      'male' => 'Male',
      'female' => 'Female'
    );
}
Code:
echo form_dropdown('gender', $gender_options, set_value('gender', $gender_answer));

#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.

#10
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.