I’m having a user registration form with a select field, When ever I get an error on the form user redirected to the form and display the previously entered values
I able to display old values for the text fields but I am finding it difficult to display old values for the select field,
My select field as follows,
<select id="app-subdomainsuffix" class="form-control @error('subDomainSuffix') is-invalid @enderror" name="subDomainSuffix" aria-required="true"> <option value="">- {{ __('sentence.Select domains') }} -</option> <option value="test.site" >TEST.SITE</option> </select>
I’m using laravel 7
Advertisement
Answer
First of all you need value for each option and i don’t see any value for your first option. Then you need a logic to check if the option was selected and assign “selected Attribute” to HTML :
<select id="app-subdomainsuffix" class="form-control @error('subDomainSuffix') is-invalid @enderror" name="subDomainSuffix" aria-required="true"> <option value="value1" @if(old('subDomainSuffix') == "value1") selected @endif >- {{ __('sentence.Select domains') }} -</option> <option value="test.site" @if(old('subDomainSuffix') == "test.site") selected @endif >TEST.SITE</option> </select>
Above example can just demonstrate how to select the option, but for cleaner code you better come with array of option as mentioned in this topic .