bootforms
bootforms copied to clipboard
Empty Option
Hi,
How to set empty option in select?
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.