Laravel, default value ignore with variable - php

// EDIT
It was a problem in my controller and it specific in my project. My bad, sorry
// END EDIT
I'm pretty lost with something and I need help, this project was not done by me, I just need to do a quick fix.
I have a select in a form :
{{ Form::label('option', 'Type de contrat') }}
{{ Form::select('option[]', $contracts->lists('option', 'id')->toArray(), old('option')[0], ['class' => 'form-control selectpicker']) }}
When my form is not fill correctly, I'm redirect to the form page and I want the select have the previous value. It should be work with the "old".
But it select the first value.
So I tried var_dump(old('option')[0]) which return me "16" (string variable) who is the id of the previous selected and the value of the option who need to be selected.
But if I try (replace the variable by hardcoded value):
{{ Form::label('option', 'Type de contrat') }}
{{ Form::select('option[]', $contracts->lists('option', 'id')->toArray(), 16, ['class' => 'form-control selectpicker']) }}
It's work and I don't understand why ...
I try :
(int)old('option')[0]
But doesn't work
Laravel 5.2
Why it's work with a hardcoded int but now with a variable who return a int number ?
Select generate :
<select class="form-control selectpicker" name="option[]" tabindex="-98">
<option value="13">CDD</option><option value="14">CDI</option>
<option value="15">Stage</option>
<option value="16">Apprentissage</option>
<option value="17">Freelance</option>
</select>

Since it's not a multi-select, you don't need the [] in the name of the input,
You need to use it like this:
{{ Form::select('option', $contracts->lists('option', 'id')->toArray(), old('option'), ['class' => 'form-control selectpicker']) }}

If you have more than one, then you can try it:
view
#for($i = 1; $i <= 2; $i++)
<input type="hidden" name="count_select[]">
<select class="form-control selectpicker" name="option{$i}" tabindex="-98">
<option value="13"{{ (old("option{$i}") == 13) ? ' selected' : '' }}>CDD</option>
<option value="14"{{ (old("option{$i}") == 14) ? ' selected' : '' }}>CDI</option>
<option value="15"{{ (old("option{$i}") == 15) ? ' selected' : '' }}>Stage</option>
<option value="16"{{ (old("option{$i}") == 16) ? ' selected' : '' }}>Apprentissage</option>
<option value="17"{{ (old("option{$i}") == 17) ? ' selected' : '' }}>Freelance</option>
</select>
#endfor
controller
$count_select = $request->input('count_select');
if(count($count_select) > 0){
for($a = 1; $a <= count($count_select); $a++){
$this->validate($request, [
"option{$a}" => 'required'
]);
}
}

Related

How to do show selected option in the phalcon volt syntax

I have an html code something like
<label>Is Pan Verification Enabled</label>
<select class="form-control" name="ispanverified">
<option >Select</option>
<option selected={{ data['ispanverified'] == 'true' ? 'selected' : '' }} value="true">True</option>
<option selected={{ data['ispanverified'] == 'false' ? 'selected' : 'false' }} value="false">False</option>
</select> <br/>
What I want to do is to show the default selected option in the select tag ,but its not working ,can anyone point me out to the right syntax?

Getting selected option on edit form in laravel 6

i want to edit user profile which has a country, country field is a select option, i want to get a user's country value in select field, i tried the answer in stack but it didn't work for me knowing there is relationship between user and country belongsTo hasMany.
edit.blade.php
<select id="country" name="country_id" class="form-control" >
<option value="" selected disabled>Select</option>
#foreach($countries as $country)
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
#endforeach
</select>
usersController.php
public function edit($id)
{
$users = Auth::user();
$countries = Country::all();
return view('users.edit')->with([
'users' => $users,
'countries' => $countries
]);
}
Instead of this:
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
I believe you want this:
<option value="{{$country->id}}"{{ $country->id == $users->country_id ? ' selected' : '' }}> {{ $country->name }}</option>

How to insert multiple values to DB using select2 multiple in Laravel 7

When I'm trying to insert all selected values into the DB I get [ result in driver_lisence column.
Here is my blade
<div class="form-group col-sm-6">
<label>{{__('driver.driver_licence')}}</label>
<select name="driver_lisence[]" class="form-control select multiple" id="" multiple="multiple">
<option {{ old('driver_lisence') == 'A' ? 'selected': '' }} value="A">A</option>
<option {{ old('driver_lisence') == 'B' ? 'selected': '' }} value="B">B</option>
<option {{ old('driver_lisence') == 'C' ? 'selected': '' }} value="C">C</option>
<option {{ old('driver_lisence') == 'D' ? 'selected': '' }} value="C">D</option>
<option {{ old('driver_lisence') == 'E' ? 'selected': '' }} value="E">E</option>
</select>
#error('driver_lisence')
<label id="with_icon-error" class="validation-invalid-label" for="with_icon">
{{$message}}
</label>
#enderror
</div>
Here is my Model
protected $fillable = [
'firstname',
'lastname',
'birthday',
'email',
'phone',
'password',
'gender',
'image',
'pasport_id',
'fin_code',
'driver_lisence',
'status',
'fcm_token',
'driver_group_id',
'app_version',
];
protected $casts = [
'driver_lisence' => 'array'
];
Here is my Controller
public function store(DriverRequest $request)
{
$data = $request->all();
if ($request->file('image'))
{
$data['image'] = request('image')->store('',['disk' => 'uploads']);;
}
$data['password'] = Hash::make($data['password']);
Driver::create($data);
return redirect(route('admin.driver.index'))->with(_sessionmessage());
}
I just want to select all values and insert into driver_licence column like array.
Fixed by my self. problem was in my migration. there im inserted column lenth 1. i deleted it and i works

Laravel: Add 'selected' attribute to HTML select option, either from an older value or the db

I'm using laravel 7 for my web-app in which I have the same route both for creating and editing a project.
When I edit a project, I need to be able to load my view with the project's database values.
But when I change some fields and try to submit the form, in case I get a validation error, the expected behavior is to reload the view with the 'old' values, and if there aren't old values (which means that I have not changed anything in the corresponding fields), then with those that come from the db.
The problem is that I don't seem to be able to find a good way to maintain the 'old selected' attribute
after failing validation.
<select id="client" name="client" type="select">
<option {{(isset($project) && $project->client == 'o1') || old('client') == 'o1' ? 'selected' : ''}} value="o1">o1</option>
<option {{(isset($project) && $project->client == 'o2') || old('client') == 'o2' ? 'selected' : ''}} value="o2">o2</option>
<option {{(isset($project) && $project->client == 'o3') || old('client') == 'o3' ? 'selected' : ''}} value="o3">o3</option>
So for example if I have o3 in my db when I load the project to edit it, change it to o1, fail validation on another field, when the view reloads, instead of o1, o3 will be selected. Any suggestions?
You can do it like below:
#php
$client = (isset($project)) ? $project->client : '';
if ($errors->any())
{
$client = request()->old('client');
}
#endphp
<option {{ $client == 'o1' ? 'selected' : ''}} value="o1">o1</option>
<option {{ $client == 'o2' ? 'selected' : ''}} value="o2">o2</option>
<option {{ $client == 'o3' ? 'selected' : ''}} value="o3">o3</option>
You no need to write long conditions, you can do like below.
<option {{ old('client', $project->client) == 'o1' ? 'selected' : '' }} value="o1">o1</option>
<option {{ old('client', $project->client) == 'o2' ? 'selected' : '' }} value="o2">o2</option>
<option {{ old('client', $project->client) == 'o3' ? 'selected' : '' }} value="o3">o3</option>
You could use an #if to see if the option is already set when the page re-loads or fails your validation..
<option value="">Select an answer...</option>
<option value="o1"
#if($project->client == 'o1' || old('client') == 'o1')
selected="selected"
#endif()
>o1</option>
This has always worked for me. I think it might have something to do with your isset() since it's not testing to see if the variable is set, but rather what the value actually is.

Set Default option in dropdown list HTML

In my create.blade.php I have a dropdown list which is like that.
<div class="form-group">
<label for="company-content">Sexe</label>
<select name="sex" id="" class="form-control">
<option value="">Choice</option>
<option>Women</option>
<option>Man</option>
</select>
</div>
If I choose the option 2, that is to say the item man, in my edit.blade.php I will wish to get the item man.
However, when I want to change the item, my dropdown list is always on woman bij default. It's not practical...
Here is my code concerning the file edit.blade.php, do you have an idea please?
Thank you
<div class="form-group">
<label for="company-content">Sex</label>
<select name="sexe" id="" class="form-control">
<option>Women</option>
<option>Man</option>
</select>
</div>
Edit: 16/03/2019
Visibly, I should do several modifications on my Controller too?
My function edit
public function edit($id)
{
//
$candidats = Candidat::find($id);
$permis = Permis::all();
return view('admin.candidats.edit', compact('candidats', 'permis'));
}
My function update
public function update(Request $request, $id)
{
$request->validate([
'sexe' => 'required|string',
'fk_permis' => 'required'
]);
$candidats = Candidat::find($id);
$candidats->sexe = $request->get('sexe');
$candidats->fk_permis = $request->get('fk_permis');
$candidats->save();
return redirect()->route('candidats.index')
->with('success', 'mise à jour effectuée');
}
Where I should add this line please?
return view('admin.candidats.edit', ['data' => $data]);
Here is my edit.blade.php
<div class="form-group">
<label for="company-content">Sex</label>
<select name="sexe" class="form-control" >
<option value="Man" {{ $data['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $data['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
</div>
So, my problem is in my Controller?
for laravel from controller return view with data like
return view('edit', ['data' => $data]);
<select name="sexe" class="form-control" >
<option value="Man" {{ $data['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $data['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
and for php
<select name="sexe" class="form-control" >
<option value="Man" {{ $_GET['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $_GET['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
Assuming you're POSTing your data in a form, and have included values as suggested by #Second2None, you need to tell your form to select the relevant option.
<option<?php if ($_POST['sexe'] == 'man') echo " selected"; ?>>Man</option>

Categories