bootforms icon indicating copy to clipboard operation
bootforms copied to clipboard

Empty Option

Open hugofabricio opened this issue 8 years ago • 1 comments

Hi,

How to set empty option in select?

hugofabricio avatar May 15 '16 13:05 hugofabricio

If you want an empty or "null" option in a select list, you need to include an empty option. This is external to BootForm, it's just how HTML works.

Example from a random piece of my code, prepared to :

$data['offices_list'] = ['' => ''] + Office::orderBy('name')->pluck('name', 'id')->toArray();

Then you'd load up the $offices_list array using BootForm. Default option = first option = blank. If validation fails, on reload the default will be whatever the user entered last time, assuming the select field is normal HTML without JavaScript fanciness.

Keep in mind that means the user can easily submit a form with this blank field selected, so make sure you perform necessary validation server-side before saving, which you should be doing anyway to protect from shenanigans.

GregPeden avatar Jun 09 '16 20:06 GregPeden