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
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 .
<option value="test.site" {{ (old("subDomainSuffix") == 'test.site' ? "selected":"") }}>TEST.SITE</option>
Related
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...
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.
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"
{{ (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.
Hye,
I want to get dropdown selected data.
For your information, my dropdown data is from my database. I already retrieve data from database and put it inside my dropdown.
Right now, I want to make validation. If user key in all information and forgot to key in 1 column data, the dropdown part should get the previous selected data right? So here are my coding, I still can get the previous selected data.
<div>
<x-label for="pizzatype" :value="__('Choose pizza type:')" />
<select name="pizzatype" id="pizzatype">
<option selected disabled>Please choose</option>
#foreach ($pizzainfo as $pizzaitem)
<option value="{{ old('$pizzaitem->name') }}">{{ $pizzaitem->name }}</option>
#endforeach
</select>
</div>
All dropdown button should get the selected previous button...
As I understand the problem is that you are trying to get an old() value that does not exist...
the quick fix is :
<select name="pizzatype" id="pizzatype">
<option #if(!old('pizzatype')) selected #endif disabled>Please choose</option>
#foreach ($pizzainfo as $pizzaitem)
<option #if(old('pizzatype') == $pizzaitem->name) selected #endif value="{{ $pizzaitem->name }}">{{ $pizzaitem->name }}</option>
#endforeach
</select>
I'm working on validating my registration form.
If the input is a regular input, i can populate the previous value if it fails like so
<input type="tel" name="phone" value="{{old('phone')}}">
How do I populate the value on a select option?
<div class="input--select">
<label>Age Range <span>*</span></label>
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
<option selected disabled>Select your age range</option>
<option value="18-25">18-25</option>
<option value="26-35">26-35</option>
<option value="36-45">36-45</option>
<option value="46-55">46-55</option>
<option value="56-65">56-65</option>
<option value="66-75">66-75</option>
<option value="75+">75+</option>
</select>
#if ($errors->has('age_range')) <span class="error-message">Age range option is required.</span> #endif
</div>
I suggest putting all you ages in an array and pass that array to view:
$ages = ['18-25','25-50','50-75','75+']; // More dynamic and you can extend in anytime
Now changes in html :
<div class="input--select">
<label>Age Range <span>*</span></label>
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
// 1. first change which is creating your problem
<option {{ old('age_range') ? "" : "selected" }} disabled>Select your age range</option>
// 2. This is just optimization for short code
#foreach($ages as $age)
<option {{old('age_range') ==$age" ? $selected : ""}} value="{{$age}}">{{$age}}</option>
#endforeach
</select>
#if ($errors->has('age_range')) <span class="error-message">Age range option is required.</span> #endif
</div>
So in point 1 you are always applying the selected with first option.
Suppose your old value was 46-55
your html looks like :
<select name="age_range" class="age-range" value="{{old('age_range')}}" required>
<option selected disabled>Select your age range</option>
<option value="18-25">18-25</option>
<option value="26-35">26-35</option>
<option value="36-45">36-45</option>
<option selected value="46-55">46-55</option>
<option value="56-65">56-65</option>
<option value="66-75">66-75</option>
<option value="75+">75+</option>
</select>
if you take a look at above html there are 2 selected options. Html always picks the first one this is what creating the problem.
Point 1. will check if there is a old value available it will not apply the selected to the first placeholder option.
{!! Form::select('name', $varableyouwanttodisplay, old('name'),
['id'=>'id', 'class' => 'form-control select2', 'data-
placeholder' => 'select','required', 'style' => 'width:100%']) !!}
try like above code.
#php $selected = old('experience') ?: 66; #endphp
and than
{{ Form::select('experience', $experience, $selected, ['class' => 'form-control']) }}
This works in a case when a blade template loads for the first time and doesn't have 'old' value and you need predefined selected option(I.e. 66) for that case.
I have a page where user can edit their profile settings. Data are read from db and displayed as default values for many fields like email, city, salutation, first name, last name etc...
But for country field I am getting the list from DB and always the first option is showed (in this case Afghanistan). How can I set this field to default value if user "preselected" the country before? For example if user selected and saved Germany before, I want this option to be shown as default, not Afghanistan.
{!!Form::open(array('url' => 'editUserProfile/'.$user->id, 'files' => true))!!}
<input class="form-control" type="text" name="firstName" value="{{$user->firstName}}" required>
<select class="form-control" name="country">
#foreach($countries as $country)
<option>{{$country->country_name}}</option>
#endforeach
</select>
{!! Form::close() !!}
How can I set $user->countryas default like for a first name?
Here's some code:
<select class="form-control" name="country">
#foreach($countries as $country)
<option value="{{$country->country_name}}" {{ $country->country_name == $user->country? ' selected' : '' }}>{{$country->country_name}}</option>
#endforeach
</select>
I added value attribute so as country name can be passed to server.
And added ternary operator for checking if current $country->country_name equals to $user->country.
This might help:
#foreach($countries as $country)
<option value="{{ $country->id }}" <?php if($user->country_id == $country->id){ echo "selected"; } ?> >{{$country->country_name}}</option>
#endforeach
I want to repopulate the old value after validation.I have tried many options and I am not getting.
Here is my code
select name="Skills[]" class="form-control select2" multiple="multiple" id ="Skills"
#foreach($skills as $skill)
option value="{{$skill->skill_id}}" >{{$skill->skill_name}}</option>
#endforeach
/select
correct me if I'm wrong but what you mean by repopulate the old value is to show as selected the option chosen by the user after validation right?
if so then you can try after redirecting to a View the following:
<?php $i=1; ?>
<select name="Skills[]" class="form-control select2" multiple="multiple" id ="Skills">
#foreach($skills as $skill)
<option value="{{$skill->skill_id}}" #if($skill->skill_id==$i++){'selected="selected"'}#endif>{{$skill->skill_name}}</option>
#endforeach
</select>