adamwathan / bootforms

Rapid form generation with Bootstrap 3 and Laravel.
MIT License
417 stars 103 forks source link

Empty Option #102

Open hugofabricio opened 8 years ago

hugofabricio commented 8 years ago

Hi,

How to set empty option in select?

GregPeden commented 8 years ago

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.