Default value on form select (with foreach loop) - forms

So, I want to achieve a <select> like solution where a default value is equal to the value that is currently in the database, so the 'old value'.
1
I have this, where I loop a foreach to fill <option>.:
<select id="size" name="size" class="form-control">
#foreach ($sizes as $s)
<option value="{{ $s->value }}">{{ $s->name }}</option>
#endforeach
</select>
Now where I want to use this is an edit-form in laravel 5.2 where it fills in this form with the current values from the database for the selected user. My input fields with text fill in correctly but this select doesn't.
2
Working with Laravel forms I know this is possible to set the default value to S. But I don't know how to loop this is foreach...:
{{ Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S') }}
So I need to find a way to set default value with HTML-select tags while in a loop with values from my controller or find a way to loop in Laravel Forms.. Either is good, as long as it works

Don't know if this is what You are asking, but if You know current value, You can check it with if statement:
<select id="size" name="size" class="form-control">
#foreach ($sizes as $s)
#if ($s->value == $currentSize)
<option selected value="{{ $s->value }}">{{ $s->name }}</option>
#else
<option value="{{ $s->value }}">{{ $s->name }}</option>
#endif
#endforeach
</select>

Related

insert data if not exist in the column using Laravel elequant

I'm developing a doctor's appointment laravel project. the condition is if a user fixes an appointment they can't able to fix the appointment for the same doctor at the same date and time. I tried with firstOrCreate method but doesn't match my condition. here are my conditions
1.if doctors_id AND date AND time already exist then shouldn't insert the data
2.if doctors_id OR date OR time, any three of this already exist then can insert the data
3.if all fields already not exist then insert data
here are the code snippets
In view
<div class="card-body">
<form action="appointment" method="post">
{{ #csrf_field() }}
<select name="doctors" id="" class="form-control">
#foreach ($docList as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
#endforeach
</select><br><br>
<input type="date" name="date" id="" class="form-control">
<br><br>
<select name="time" class="form-control">
<option value="9-10AM">9-10AM</option>
<option value="10-11AM">10-11AM</option>
<option value="1-2PM">1-2PM</option>
<option value="2-3PM">2-3PM</option>
<option value="3-4PM">3-4PM</option>
</select>
<button type="submit" class="btn btn-primary">Fix Appointment</button>
</form>
</div>
In controller
public function store(Request $request)
{
$uId = Auth::id();
$fixAppointment = Appointment::firstOrNew(['doctors_id'=>$request->doctors,'date'=>request('date')],['time'=>request('time')]);
$fixAppointment->users_id = $uId;
$fixAppointment->save();
}
Here I sorted out the issue by using where condition
In controller
$appointment = Appointment::where('date', '=', request('date'))->where('time','=',request('time'))->where('doctors_id','=',request('doctors'))->first();
if ($appointment === null)
{
$fixAppointment = New Appointment;
$fixAppointment->doctors_id = $request->doctors;
$fixAppointment->users_id = $uId;
$fixAppointment->date = $request->date;
$fixAppointment->time=$request->time;
$fixAppointment->save();
}
here is the link which I referred to find this solution.

Angular2 reactive forms select how to set invalid?

I use reactive forms within my app. In a certain form I want to display a required (Validators.required) select like this:
<select class="form-control"
[id]="dformControl.key"
[formControlName]="dformControl.key"
[multiple]="dformControl.multiple">
<option *ngIf="!dformControl.value"
value="undefined">
Choose ...
</option>
<option *ngFor="let opt of dformControl.options"
[value]="opt.value"
[selected]="dformControl.value == opt.value">
{{opt.label}}
</option>
</select>
The problem is whether I use value="undefined" or value="" the form control still is set to valid because it got a value. Do not present the value attribute results in value="Choose ...".
Am I using select with reactive forms in a false way or how would I be able to make the option "Choose ..." being not valid??
Assigning initial value of select control to null will do the trick. Try below,
model_property = null
....
this.fb.group({
....
'control_key' : [this.model_property, Validators.required]
...
})
Check this Plunker!!, Look into app/reactive/hero-form-reactive.component.ts file.
I updated the Plunker to include below and it seems to be working,
<select id="power" class="form-control"
formControlName="power" required >
// see the value is set to empty,
<option value="">Choose...</option>
<option *ngFor="let p of powers" [value]="p">{{p}}</option>
</select>
Hope this helps!!
What I do is add a blank option and when that is selected since there is no value it is not valid.
<select class="form-control"
[id]="dformControl.key"
[formControlName]="dformControl.key"
[multiple]="dformControl.multiple">
<option></option>
<option *ngFor="let opt of dformControl.options"
[value]="opt.value"
[selected]="dformControl.value == opt.value">
{{opt.label}}
</option>
</select>

Make the tags and select list active without using Form Builder in laravel?

I am unable to make a dropdown select list using the below code to generate a location select dropdown where database table =>location and column=>name
<select name="location">
#foreach ($locations as $loaction)
<option value="{{ $loaction->name }}">{{ $loaction->name }}</option>
#endforeach
</select>

Converting checkbox to select (perl)

Before:
<input type='checkbox' name='killerFeature' id='killerFeature'
<%= param('killerFeature') ? ' checked' : ''%>
/>
Now:
<select name="killerFeature" id="killerFeature" class="select">
<option value="1">Enable</option>
<option value="0">Disable</option>
</select>
How do I insert the same checked (should be 'selected' now I guess?) condition in the select now?
This is a perl app, built using Mojolicious web framework.
Many thanks for your help!
Yes, the condition should be selected (selected="selected") but i belive you already figured that out from your other post :)
<select name="killerFeature" id="killerFeature" class="select">
<option value="1" selected="selected">Enable</option>
<option value="0">Disable</option>
</select>
Also from what i saw there inst a way to create the select like you can for the input as in the below example:
<%= input 'test', type => 'text' %>
So it would be something like:
<%== param('killerFeature') eq $my_var ? ' selected="selected"' : ''; %>
Ofc you would need to replace the above to your current variables and field names.
GL:)

How do I handle a multiple select form field in Perl?

What is the best, in Perl, way to get the selected values of a multiple select form field?
<select name="mult" multiple="multiple">
<option value="1">Opt. 1</option>
<option value="2">Opt. 2</option> <!-- selected -->
<option value="3">Opt. 3</option>
<option value="4">Opt. 4</option> <!-- selected -->
<option value="5">Opt. 5</option>
</select>
I get regular form fields like this: $param1 = param('param1');
If you are using the CGI Module (and I really hope you are) then you can access the multiple values by assigning the param hash to an array and CGI does the rest. So in your example:
my #mult = $q->param('mult');
will store the selected values (2, 4) in the #mult array.