Reputation:
I have a method "displayRegistrationPage()" to display the conference registration page. In this method is returned the variable $countries that returns an array with the countries, each country appears like this in the array: "DE" => "Germany".
public function displayRegistrationPage(Request $request, $id, $slug = null)
{
$conference = Conference::find($id);
$selectedRtypes = Session::get('selectedRtypes');
$total = Session::get('total');
$countries = Facades\Countries::all();
return view('conferences.registration',
['selectedRtypes' => $selectedRtypes, 'total' => $total, 'id' => $id,
'slug' => $slug, 'countries' => $countries, 'conference' => $conference]);
}
Then in the registration page I have a form and the user needs to introduce his vat number and his country, so I have this form fields below. But if the user already have the VAT and country stored in the users table I want to show that values by default so the user dont need to introduce that values. But the user might want to introduce other values.
<div class="form-group">
<label for="vat" class="text-gray">Vat number</label>
<input type="text" name="vat" class="form-control" value="{{ (Auth::user()->VAT) ? Auth::user()->VAT : old('vat')}}">
</div>
<div class="form-group">
<label for="country" class="text-gray">Country</label>
<select class="form-control" name="country" id="country">
@if(Auth::user()->country)
<option selected="selected">{{Auth::user()->country}}</option>
@endif
@foreach($countries as $key => $country)
<option value="{{$country}}"
@if(Auth::user()->country == $country) selected="selected" @endif>{{ $country}}</option>
@endforeach
</select>
</div>
Do you know how this can be properly achieved? With the select menu above it appears the country stored in the users table. But there is an issue, because its necessary to get the key (example: "DE" and not "Germany"). But with the code above in the storeRegistration() to store the registration info, the $request->all( shows "Germany" like "country" => "Germany"
,
not "DE". And in the storeRegistration() is necessary to validate the vat considering the country using the key and not the value.
Validate Vat in the storeRegistration():
$rules['vat'] = [
function ($attr, $value, $fail) use ($request) {
if (!VatValidator::validateFormat($request->country . $request->vat)) {
$fail('Please insert a valid VAT.');
}
}];
Upvotes: 0
Views: 41
Reputation: 795
change the {{ $country }} to {{ $key }}
<select class="form-control" name="country" id="country">
@foreach($countries as $key => $country)
<option value="{{$key}}" {{ Auth::user()->country == $country ? 'selected' : '' }}>{{ $country}}</option>
@endforeach
</select>
Upvotes: 0