Laravel Collective Form checkbox always checked - php

I'm using Laravel Collective for Forms and having an issue with checkbox.
Here is what I'm doing :
{!! Form::checkbox('independent',null,['class'=>'form-control', 'required' => 'required'])!!}
I've tried changing values for "null", added one more parameters as suggested by many while googling for solution but nothing seems to be working.
If anyone know the solution or having same issue, please share.

The documentation states that the third parameter is a boolean that determines if the checkbox is checked, you have an array as the third parameter. Php interprets an array as true, this is why your checkbox is always checked.
You should add true or false as the third parameter and add the options array as a fourth parameter. This can be found in the source code on GitHub.
{!! Form::checkbox('independent', null, false) !!}

Related

Laravel 6: can set boolean to 1 but not to zero

So basically I have a favorited value for my tasks. It's named 'favorited' and has a boolean so 1/true or 0/false. (mySQL)
Currently, each row (a task) has a font awesome star, if favorited is 1, it will become fas with text gold so it'll show a gold star. If favorited is 0, it will become far, which makes it a white star with the inside empty.
<i class="{{ $task->favorited ? 'text-gold fas' : 'far' }} fa-star"></i>
It has class del_link which triggers a javaScript/jQuery function. Which submits the <a> parent form
$('.del_link').each(function(){
$(this).on('click', function(event){
$(this).parent().submit();
});
});
Now this is the form wrapped around the <a></a>
<form method="post" action="{{ route('tasks.update', $task) }}">
#csrf #method('patch')
<input type="hidden" name="favorited" value="{{ $task->favorited ? 0 : 1 }}">
<i class="{{ $task->favorited ? 'text-gold fas' : 'far' }} fa-star"></i>
</form>
So basically, if favorited === 1, then set the favorited value to 0, so when I submit the form, I set the favorited value to 0. Now this for some reason doesn't work. Whenever I press the star so I can favorite it, it works, but when I do it the other way around to unfavorite; it doesn't.
This is my code for the TaskController.php
public function update(Request $request, Task $task)
{
$task->update([
'favorited' => request()->has('favorited')
]);
return back();
}
Actually, the code above and the method is something I got from the Laravel 5.8 scratch video. (Laracasts laravel tutorial)
It has worked for me using a checkbox instead of using a star, but it doesnt work this way.
I've tried using a lengthy if statement, that if request was 1 I would set it to 1. (request = 1 means that the hidden input was 1 so it wasn't favorited) and an else, so if 0, it would set it to 0 thereby unfavoriting it.
But in that case, it still didnt work. I've dd the request, task and text to show me where I am and what I get, but to no avail.
Thanks for any help
When you use request()->has('favorited'), it return true when the request contain the input favorited=1 or favorited=0 (both cases).
It does work with a checkbox because when an input type="checkbox" is unchecked, it is not send in the request.
When it's an input type="hidden", favorited will be present in the request all the time.
You should change it to 'favorited' => (int)(bool)request()->input('favorited')
According to the Laravel documentation $request->has('favorited') checks if favorited is present in the request. Whether it's set to 0, 1, or any other value does not impact the returned value.
Instead, using (bool)$request->input('favorited') should work. Or if you haven't cast the favorited attribute to a boolean (bool)$request->input('favorited') ? 1 : 0.
If this doesn't work, the value isn't set properly in the request, so it's most likely a front-end issue. In that case, dd($request->input('favorited')) to see if the value is present as expected. I don't immediately see an issue with your view and js.
and welcome to Stack Overflow.
Your problem is on both, frontend and backend:
Backend: request()->has('field');
The request will always have this field which means the value of it is 1 all the time since you have input field presented.
Frontend: You are not changing the value of the input.
Two fixes for this:
1. Frontend you can set Javascript to change the input value when you press this to the opposite.
Do the same as #1 on the backend, which is easier.
So, this should work for you:
// First validate you have that field.
request()->validate([
'favorited' => ['required'],
]);
// Then set the favorited to opposite of current.
$task->update([
'favorited' => !$task->favorited // notice the "!" (take oposite value)
]);
Edit:
By the way you can get rid of Javascript by removing input hidden and making submit the value you want to use, since its either 0 or 1.
So: remove the input type="hidden", and change from a-href to button.
<button name="favorited">
<i class="{{ $task->favorited ? 'text-gold fas' : 'far' }} fa-star"></i>
</button>
That should do the thing.

Laravel: set checkboxes as default set

I have one really simple question. I have a form with three groups of dynamically generated check boxes. Let's say one of these groups of check boxes needs to have all check boxes as default set.
This is not a problem, the problem comes when I submit the form and I need to have the state of the checkbox.
Here is the HTML code I generate:
#for($i=0;$i<24;++$i)
{!! Form::checkbox('check_hour[]', $i, $check_hour[$i], ['class' => 'check_hour']) !!} {{($i+1)}}h
#endfor
and here is the code in the Controller:
for($i=0;$i<24;++$i) $check_hour[$i] = (isset(Input::get('check_hour')[$i])) ? true : false;
In this situation I have no problem to save the checkbox state, but I need all check boxes to bet set as default.

CakePHP Form->postLink does not work for the first row [duplicate]

Using code that has been baked into CRUD, I have the following code for deleting an item:
<?php echo $this->Form->postLink(__('Delete'), array('controller'=>'attachments', 'action' => 'delete', $attachment['Attachment']['id']), null, __('Are you sure you want to delete "%s?"', $attachment['Attachment']['name'])); ?>
The problem is that it lies wrapped in a FORM tag, and so what ends up happening is Cake doesn't include the Form that the postLink would submit.
Is there another way that still holds true to the integrity of Cake's infrastructure that would work even when I increase the security settings? Probably needs to be a link to /attachment/delete/id, but baking for some reason chose to create a form and post it vs. creating a link, so I figured there was a reason for that and if so I want to uphold that reason.
You probably didn't read the warnings in the doc block regarding this method
(http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink)
This method creates a <form> element. So do not use this method inside an existing form.
Instead you should add a submit button using FormHelper::submit()
So don't do that. You would need to write to a buffer and output later (see this closed PR).
I have the same problem in cakephp 3.0 $this->Form->postLink() was not working for first entry.
Then i do some R&D but not found any useful. Then i make some changes in form tag and $this->Form->postLink(). I remove the $this->Form->create() from the .ctp files and use only $this->Form->postLink(); and it start working.
Do not use the $this->Form->postLink() inside any other form tag
i.e $this->Form->create(null, ['url' => ['action' => 'ExportCustomers']]);
If you want both then you have to adjust the $this->Form->postLink() and form tag according to it will not effect each other.

laravel read a checkbox value in a controller

This is my html blade code
{{Form::checkbox('remember_me', '', array('id'=>'remember_id'))}}
<label for="remember_id">Remember me</label>
This is my controller code:
echo Input::get('remember_me');exit;
The result is always empty, why please?
The checkbox is always checked when I run the page, why please?
Thanks
Please have a look on the [parameter list of the Form::checkbox() method][1].
The second parameter is your checkbox value. You manually set it to an empty string. Set it to null in order to keep the browsers default values (laravel default is 1). The third parameter is a boolean. Set it to true to check the box and false to uncheck it.
The fourth parameter is your options array where you can specify your id. So the correct method call should be:
{{Form::checkbox('remember_me', null, false, array('id'=>'remember_id'))}}
Update:
Checkboxes that are not checked, will not be included in your POST data. So the only reliable way to verify that a checkbox has been checked is to check if it is set. That can be done using isset() with regular PHP functions, or if laravel is being used, by using Input::has() which returns a boolean dependent on whether your input data contains a given key.
You did not add a value to the checkbox
{{Form::checkbox('remember_me', 'value goes here', true, array('id'=>'remember_id'))}}
The second param is the value
Normally I write the checkbox without blade and I can do with it whatever I want, like normal HTML. I don't see why you can do it the normal HTML way, because it always ends up doing the same thing you expert.
{!! Form::label('Test-2') !!} {!! Form::checkbox('ch[]', 'value-2', false); !!}

array filed values not working in Laravel blade template system?

I have 2 email fields and I am using
{{ Form::text('email[]', Input::old('email'),array('class' => 'large-2', 'placeholder' => 'email address','id'=>'email')) }}`
if I use [] to get multiple values for same variable, it is giving error in view page if posted back some data. for example if some fields are mandatory and if user fail to fill those, page will be redirected to same view page from where it was launched.
In such cases it is showing error.
How to fix this issue?
One text field can contain only one data. So, you need another text field to achieve that. if you don't want to show the multiple emails, you can use hidden fields.
{{ Form::hidden('email[]', Input::old('email1'))
{{ Form::hidden('email[]', Input::old('email2'))
Or you can use select. Laravel allow array data if use select field.
For example:
{{ Form::select('size', array('L' => 'Large', 'S' => 'Small')) }}
http://laravel.com/docs/html#drop-down-lists

Categories