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

Time:08-24

I followed this Before Edit

After edit

The string inputs is as follows:

<input type="text  name="claimedBy" value="{{ old('claimedBy', $lostFound->claimedBy) }}">

But the select is like this:

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

CodePudding user response:

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  

new

type="text"  

CodePudding user response:

{{ (old('handover') ? old('handover') : $lostFound->handover ?? '') == $lostFound->handover ? 'selected' : '' }}

What is doing here is: if (if old(handover) true old(handover) else $lostFound->handover default '') equals $id true selected false ''

also remove disabled, and it is important that u set '' for value of the input so that u can check for it and return proper error if needed. What i like to do is prepend option with '' value with text like "Please select one" and then u can always check for it if it wasn't selected. Then u could use disabled approach, but with setting old value as selected and disabled it won't post value.

  • Related