Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel 8: old() helper is not functioning correctly for <select>

I followed this guide of simplifying the controller. Sharing one form between create and edit

The old() helper is not working as expected with the <select>.
Every time I submit the form when something is missing, the old selected option is there, but when I resubmit the form, it shows an error that the <select> field is required…

Before Edit

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

After edit

The string inputs is as follows:

<input type="text class="form-control @error('claimedBy') is-invalid @enderror" name="claimedBy" value="{{ old('claimedBy', $lostFound->claimedBy) }}">

But the select is like this:

<select class="form-select select-basic-single @error('handover') is-invalid @enderror" name="handover">
  <option value="{{ old('handover', $lostFound->handover ?? 'null') }}" selected disabled>{{ old('handover', $lostFound->handover ?? 'Handed to') }}</option>
  <option value="police"> Police </option>
  <option value="owner"> Owner </option>
  <option value="warehouse"> In Warehouse </option>
</select>

Make the option value null and the text Handed to if no value is selected.

>Solution :

Try removing the disabled from the option. A disabled option will not be submitted.

There is a missing comma in your input field by type="text"

old

type="text class="form-control @error('claimedBy') is-invalid @enderror" 

new

type="text" class="form-control @error('claimedBy') is-invalid @enderror" 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading