susanBuck / e15-spring22

0 stars 0 forks source link

How to get old form data for select input? #35

Closed pllealfunes closed 2 years ago

pllealfunes commented 2 years ago

Hi guys,

I was wondering if anyone knew how I can get old form data for a select input? I have tried the below and no luck. It keeps going back to my default value. Thanks!

Helper in select input

      <select name="categories" id="categories" value='{{ old('categories') }}'>
                      <option selected disabled>Select a Category</option>
                      <option value="physical">Physical</option>
                <option value="creative">Creative</option>
                <option value="mental">Mental</option>
                <option value="food">Food</option>
                <option value="collecting">Collecting</option>
                <option value="games">Games/Puzzles</option>
        </select>

Helper in each option

  <select name="categories" id="categories">
                        <option value="physical" value='{{ old('categories') == 'physical' ? 'selected' : '' }}'>Physical
                        </option>
                        <option value="creative" value='{{ old('categories') == 'creative' ? 'selected' : '' }}'>Creative
                        </option>
                        <option value="mental" value='{{ old('categories') == 'mental' ? 'selected' : '' }}'>Mental</option>
                        <option value="food" value='{{ old('categories') == 'food' ? 'selected' : '' }}'>Food</option>
                        <option value="collecting" value='{{ old('categories') == 'collecting' ? 'selected' : '' }}'>Collecting
                        </option>
                        <option value="games" value='{{ old('categories') == 'games' ? 'selected' : '' }}'>Games/Puzzles</option>
  </select>
susanBuck commented 2 years ago

Hi @pllealfunes -

Instead of addingselected to the value, add it as another attribute in the element.

Here's a simplified example:

<select>
    <option value='a' {{ old('categories') == 'A' ? 'selected' : '' }}>A</a>
    <option value='b' {{ old('categories') == 'B' ? 'selected' : '' }}>B</a>
    <option value='c' {{ old('categories') == 'C' ? 'selected' : '' }}>C</a>
</select>

It's similar to our checkbox example, except instead of adding checked we're adding selected.

Let me know if you have any issues getting that working!

pllealfunes commented 2 years ago

@susanBuck It worked.Thank you!