I have been in little weird situation. I am passing value as 0 when checkbox is checked in a particular scenario. But, during update or during any error scenario, the value returned be 0 and by default with value 0 checkbox is not being checked.
<input type="checkbox" {{ old('day_0', $data->day_0 == 0) ? 'checked' : '' }} class="onoffswitch-checkbox form-control #error('day_0') is-invalid #enderror" id="day_0" name="day_0" value="0">
I know this might be little weird to pass value as 0 but is there any way to tackle with this by passing value 0
Your parenthesis for the old() function doesn't match up to be valuables, with your current code, the second argument becomes an expression $data->day_0 == 0 - meaning you end up getting old('day_0', true) == 0 if you use the fallback value and that value was 0.
The function old() will use the old value if it existed, otherwise $data->day_0 as a default value if not.
<input type="checkbox"
{{ old('day_0', $data->day_0) == 0 ? 'checked' : '' }}
class="onoffswitch-checkbox form-control #error('day_0') is-invalid #enderror"
id="day_0"
name="day_0"
value="0">
just use like shown below
<input type="checkbox" {{ old('day_0', $data->day_0) === 0 ? 'checked' : '' }}
class="onoffswitch-checkbox form-control #error('day_0') is-invalid #enderror"
id="day_0" name="day_0" value="0">
Related
I have a problem that seems like a bug with the Laravel checkbox old() value. I have a code where I want to fill default values on edit and, if the form submit fails, I want to fill old() value to the form.
The problem is when the original database value is TRUE and the new form submit is false for the checkbox. Laravel old() does not catch the FALSE value on submit for checkbox and fills the original value which is TRUE.
<input name="active" type="checkbox"
#if( old('active', $voucher->active ?? false) ) checked='checked' #endif
>
It falls back to the default value if submit is FALSE. All solutions I found are wrong. How can I solve it?
I think it should be
<input name="active" type="checkbox" value="{{old('active', $voucher->active ?? 0)}}" {{isset($voucher->active)?'checked':''}}>
still want to check if its true or not or 1 or 0 you can check like below
<input name="active" type="checkbox" value="{{old('active', $voucher->active ?? 0)}}" {{(isset($voucher->active)&&$voucher->active==true)?'checked':''}}>
As per me.for your requirement checkbox is not suitable one .Better you have to go for radio button .So user can choose true or false on active
Try this
<input type="checkbox" class="custom-control-input" name="active" value="1"
#if((old('_token') && old('active') != null) || (old('_token') == null && $voucher->active))
checked
#endif
>
So the solution would be to test another field like #old('submit')
<input name="active" type="checkbox"
#if( old('active') || (!old('submit') && $voucher->active) ) checked='checked' #endif
>
which is send by submit. It seems it works but dont know if it is always send or if there is a use case where submit could be empty.
I’m new to both Laravel and php. Need some help with generating random numbers.
If checkbox 1 (exercise_choice_10) is selected a random number between 1-10 shall be generated.
If checkbox 2 (exercise_choice_20) is selected a random number between 11-20 shall be generated.
If checkbox 3 (exercise_choice_30) is selected a random number between 21-30 shall be generated.
<input type="checkbox" name="exercise_choice_10" value="10" {{ app('request')->input('exercise_choice_10') ? 'checked' : '' }}> Type 1</input></BR>
<input type="checkbox" name="exercise_choice_20" value="20" {{ app('request')->input('exercise_choice_20') ? 'checked' : '' }}> Type 2</input></BR>
<input type="checkbox" name="exercise_choice_30" value="30" {{ app('request')->input('exercise_choice_30') ? 'checked' : '' }}> Type 3</input></BR>
<form action="{{ route('exam.robot', ['course' => $course]) }}" method="post">
#csrf
<input type="checkbox" name="exercise_choice_10" value="10" {{ app('request')->input('exercise_choice_10') ? 'checked' : '' }}> Type 1</input></BR>
<input type="checkbox" name="exercise_choice_20" value="20" {{ app('request')->input('exercise_choice_20') ? 'checked' : '' }}> Type 2</input></BR>
<input type="checkbox" name="exercise_choice_30" value="30" {{ app('request')->input('exercise_choice_30') ? 'checked' : '' }}> Type 3</input></BR>
</BR>
<button class="button">Submit</button>
</BR>
</BR>
{{ $random }}
</form>
In your controller, you can check if the checkbox was checked, and use the value from that checkbox. We know from your examples that the range will be between $value - 9 and $value.
We can use rand($min, $max) to get the random value.
if ($request->has("exercise_choice_10")) {
$value = $request->input("exercise_choice_10");
$randomValue = rand($value-9, $value);
}
Repeat the same for your other checkboxes and return the $randomValue to your view.
Keep in mind, by using checkboxes, all 3 can be checked at any given time - so you may want radio-buttons instead (where only one per name can be checked).
Thank you for your reply Qirel :)
This solution (see below) did only work with radiobuttons. When using checkbox the last checked checkbox will override the rest. So Im still working on a solution for checkbox.
if ($request->input('exercise_choice_10')) {
$value = $request->input("exercise_choice_10");
$random = rand($value-9, $value);
}
else {
}
if ($request->input('exercise_choice_20')) {
$value = $request->input("exercise_choice_20");
$random = rand($value-9, $value);
}
else {
}
if ($request->input('exercise_choice_30')) {
$value = $request->input("exercise_choice_30");
$random = rand($value-9, $value);
}
else {
}
I want to make an if condition inside the min="" attribute of an input element.
Here is my code:
<input type="number" id="qty" name="qty" min="1" max="{{ $product_info[0]->quantity - $sessionQty }}" step="1" value="1" readonly>
What I want is to be able to write a condition like:
min = "if($product_info[0]->quantity - $sessionQty == 0) {
echo 0;
} else {
echo 1;
}"
When my $product_info[0]->quantity - $sessionQty would be equal to zero, the minimum value should be set to 0. when the condition is not zero, the minimum value should be set to 1.
Is this possible?
I am assuming it is php code that you are putting inside min in that case this is the working code as below
min="<?php echo ($product_info[0]->quantity - $sessionQty) == 0 ? '0' : '1'; ?>"
if {{}} is from some templating framework then it should also work like below
min="{{ ($product_info[0]->quantity - $sessionQty) == 0 ? '0' : '1' }}"
I am using select2 for selection of users, I am populating select2 according to its value in database, I am doing it like this :
<input class="form-control" data-tag-select="director" remote-url="{{ route("query::applies")}}" name="agency_director" type="hidden" value="{{ $team->name }}">
In the value simply i am adding the variable {{ $team->name }} which have the value but nothing comes up.
What am i missing ?
The following is my laravel blade code. I want automatically check the radio with lowest value.
{
#foreach($service_stages as $ss)
<input class="" id="money" name="money" type="radio" id="" value="{{$ss->money}}" onclick="generateDate()" checked>{{$ss->money}}
#endforeach
}
First write some code to find the smallest value (e.g. in your controller). Then save it to a variable which you provide to your blade view. Lets assume you have it in the variable $smallestValue then you can try the following:
#foreach($service_stages as $ss)
{{ Form::radio('money', $ss->money, ($ss->money == $smallestValue) ? true : false , ['id' => 'money', 'onclick' => 'generateDate()']) }}
#endforeach
This will set the checked="checked" if $ss->money == $smallestValue.