When I uncheck a checkbox and validation fails I expect that checkbox remains unchecked, instead it is checked.
My checkbox:
<input type="checkbox" name="member"
{{ ($mode == 'edit' && $user->member == 1) ? 'checked' : '' }}
{{ (old('member') == 'on') ? 'checked' : '' }} />
Where $mode == 'edit' is passed from controller to indentify the case when I'm editing the form and then to populate form fields.
It seems that when checkbox is unchecked the relative old() doesn't exist.
I tried a lot of solutions here on Stack but none works. Notice: I'm using Laravel 5.6
I've solved it by testing the existence of old('_token') (the CSRF token) as follows:
<input type="checkbox" name="member"
#if ((!old('_token') && $mode == 'edit' && $user->member == 1) || (old('member') == 'on'))
checked
#endif />
Change it to
<input type="checkbox" name="member" {{ (($mode == 'edit' && $user->member == 1) || old('member')) ? 'checked' : null }} />
Related
i am using laravel query builder but the problem is i am using radio buttons but i want to update the attendance at the radio button but it returns an error Undefined variable $attendance_status
i don't know why please help and how can i pass the $attendance_status variable
here is my code
my form
<form action="{{route('Attendances.update',$student->id)}}" method="post">
#csrf
#method('PUT')
<input type="hidden" name="id" value="{{$student->id}}">
<label class="block text-gray-500 font-semibold sm:border-r sm:pr-4">
<input name="attendences"
{{ $student->attendances()->first()->attendence_status == 1 ? 'checked' : '' }}
class="leading-tight" type="radio" value="presence">
<span class="text-success">حضور</span>
</label>
<label class="ml-4 block text-gray-500 font-semibold">
<input name="attendences"
{{ $student->attendances()->first()->attendence_status == 0 ? 'checked' : '' }}
class="leading-tight" type="radio" value="absent">
<span class="text-danger">غياب</span>
</label>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-dismiss="modal">{{trans('Students_trans.Close')}}</button>
<button class="btn btn-danger">{{trans('Students_trans.submit')}}</button>
</div>
</form>
here is my controller
update
public function update(Request $request, $id)
{
// return $request;
if($request->attendances == 'absent'){
$attendance_status = 0;
}
else if($request->attendances == 'presence'){
$attendance_status = 1;
}
Attendance::find($id)->update([
'student_id'=> $id,
'grade_id'=> $request->grade_id,
'class_id'=> $request->classroom_id,
'section_id'=> $request->section_id,
'attendance_date'=> date('Y-m-d'),
'status' => $attendance_status,
]);
return back();
You have an if and an elseif in your Controller, so only 2 conditions would create a variable named $attendance_status. You probably want to add a default branch, else basically, to make sure that the variable gets created with some default value before you try to use it in your update call.
Not sure which one of those 2 options you want to be the default but this would simplify things:
$attendance_status = $request->attendences == 'presence';
https://laravel.com/docs/9.x/redirects#redirecting-with-flashed-session-data
You may use the withInput method provided by the RedirectResponse instance to flash the current request's input data to the session before redirecting the user to a new location. Once the input has been flashed to the session, you may easily retrieve it during the next request:
return back()->withInput();
Surely {{ $student->attendances()->first()->attendence_status == 0 ? 'checked' : '' }} logic does not work in this way.
try this instead
<input name="attendences" checked="{{ $student->attendances()->first()->attendence_status == 0 ? true : false}}" class="leading-tight" type="radio" value="absent">
In my form I want the checkboxes to keep their states (checked or unchecked) after a validation error.
This is what I have so far:
<input type="checkbox" id="delete-user" class="form-check-input" data-data-section="users" name="permissions[]" value="delete-user" {{ (is_array(old('permissions')) && in_array(1, old('permissions'))) ? ' checked' : '' }}>
But it looks that the old() function doesn't work.
I also tried:
old('permissions.'.$key)
where $key is the checkbox array index but it doesn't work neither.
Any idea ?
I think you are an invalid value pass in the in_array function
Try this:
value="delete-user" {{ (is_array(old('permissions')) && in_array('delete-user', old('permissions'))) ? ' checked' : '' }}>
I have a checkbox
<input type="checkbox" name="example">
The user submits the form and return back due to failed validation so I want to show the previous state of the checkbox
<input
type="checkbox"
name="example"
{!! old('example') ? 'value="1" checked' : 'value="0"' !!}>
First question here is that, what to do with validation now? if it was 0 or 1 I would add boolean validation but now the first time it is on and after that it is 0 or 1.
Someone may say if you use that {!! old('example') ? 'value="1" checked' : 'value="0"' !!} initially, its value won't be on anymore.
So my first question changes to this: Initially the value of old('example') is null so the checkbox doesn't have checked attribute and the value of checkbox would be 0. Now when the user submits the form the old('example') has value (IT IS NOT NULL) and so this time the the checkbox has checked attribute and its value is 1 while it mustn't.
========
Another issue I encounter with is that, if old('example') has true value (which means the user had checked the checkbox), I should put something to check the checkbox. But it is not finished yet. I want to check the value of example that is in database so if the old('example') has false value(which means the user hadn't checked the checkbox) I want to check the value of example, if its value is 1 to check the checkbox and set the value of checkbox to 1 else 0. So what comes to my mind is this:
<input
type="checkbox"
name="example"
value="{!! old('example', isset($collection) ? $collection->example : null) == '1' : '1' : '0' !!}">
Now the problem is that old('example') would be on or NOTHING. And the value of $collection->example would be 1 or 0 or null. So if I compare it with '1' it won't be true even if the old('example') is true and similarly if I compare it with on, it won't be true even if the value of $collection->example is true. So let's do this:
#if(old('example') == 'on' || (isset($collection) && $collection->example))
<input type="checkbox" name="example" value="on" checked="checked">
#else
<input type="checkbox" name="example">
#endif
by this code above the issue is fixed. But is there any other better approach?
So my question is what to do with the validation in the back-end and of course what to do with check-boxes in front-end in an better approach without getting involved in jQuery and such a like?
I think it will solve your problem give it a try.
Blade file:
<input type="checkbox" name="example" value="{{ old('example') ?? '1' }}"
{{ old('example') == '1' ? 'checked' : null }} required>
The required will force the user to check the checkbox on the frontend, and on the backend, you can use the required validation because we can't trust the frontend validation.
Controller:
public function store(Request $request)
{
$request->validate([
'example' => 'required',
]);
}
I have this problem in my project in laravel.
I have a table Optional where $optional->column_name with the name of column of $bookingOptionals.
I have this code:
#foreach($optionals as $optional)
#if( $optional->column_name == 'coffee_break' ||
$optional->column_name == 'permanent_coffee' ||
$optional->column_name == 'permanent_coffeeplus'||
$optional->column_name == 'integrazione_permanentcoffee' ||
$optional->column_name == 'quick_lunch')
<div class="col-lg-3">
<fieldset>{{ $optional->nome }}</fieldset>
<input type="text" name="{{ $optional->column_name }}" value="{{ $bookingOptionals['0']->$optional->column_name }}">
</div>
#else
#endif
#endforeach
I have this error:
Trying to get property 'column_name' of non-object (View: /home/vagrant/code/prenotazioni/resources/views/dashboard/optional_booking_edit.blade.php)
use
#php
print_r($optional)
#endphp
inside your loop to see what fields it has and make sure column_name is exists,
and is $optional an object? or is it array? then you should remove raw php and probably use
$optional['column_name']
instead
I have a form with a couple of radio buttons;
<div class="form-group{{ $errors->has('procurement') ? ' has-error' : '' }} col-md-6">
<label for="procurement" class="col-md-6 control-label">Procurement Type <span class="red">*</span></label><br>
<div class="col-md-12">
<input id="procurement" type="radio" name="procurement" value="owned"> Owned
<input id="procurement" type="radio" name="procurement" value="rental"> Rental
#if ($errors->has('procurement'))
<span class="help-block">
<strong>{{ $errors->first('procurement') }}</strong>
</span>
#endif
</div>
I am reusing the form for editing purposes so I want to be able to bind the object's value for 'procurement' when I present the form in edit view. I am able to use this bind the values for text inputs;
value="{{ isset($vehicle->model) ? $vehicle->model : old('model') }}"
But this does not work for radios or selects. What should I be doing? I am NOT using the Form facade for this.
Thanks!
You can use selected and checked for what you want to do. Just add it to the end of the element, and it will select/check the element.
Something like this for input type radio:
<input id="procurement" type="radio" name="procurement" value="owned" {{ old('model') === "owned" ? 'checked' : (isset($vehicle->model) && $vehicle->model === 'owned' ? 'checked' : '') }}> Owned
<input id="procurement" type="radio" name="procurement" value="rental" {{ old('model') === "owned" ? 'checked' : (isset($vehicle->model) && $vehicle->model === 'rental' ? 'checked' : '') }}> Rental
And this for select options:
<option value="something" {{ old('model') === "something" ? 'selected' : (isset($vehicle->model) && $vehicle->model === 'owned' ? 'selected: '')}}>Something</option>
Side note: I'd recommend making sure that old('something') comes before other possible values when you're filling values in. For instance, you had:
value="{{ isset($vehicle->model) ? $vehicle->model : old('model') }}"
which would override the user's original input with database results (if any, of course) upon a failed submission. So if I was say, updating my name in a text field, and there were errors to the form, it would direct me back to the form and lose my input because the database results are before the old value. Hope that makes sense!