Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fill() isn't populating checkboxes #75

Open
ataylor32 opened this issue Jul 5, 2019 · 5 comments
Open

fill() isn't populating checkboxes #75

ataylor32 opened this issue Jul 5, 2019 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@ataylor32
Copy link
Contributor

Here is my form:

{!! Form::open()->route('posts.update', [$post->id])->fill($post)->patch() !!}
{!! Form::text('title', 'Title')->attrs(['maxlength' => '255'])->required() !!}
{!! Form::textarea('body', 'Body')->attrs(['class' => 'wysiwyg'])->required() !!}
<input type="hidden" name="allow_comments" value="0">
{!! Form::checkbox('allow_comments', 'Allow comments?', '1')->wrapperAttrs(['class' => 'wrapper-allow_comments']) !!}
{!! Form::submit('Update Post') !!}
{!! Form::close() !!}

The allow_comments field is a boolean field, as you can see in the migration file:

$table->boolean('allow_comments');

When I go to edit a post that has its allow_comments field set to 1, the checkbox renders as unchecked. I would expect it to render as checked.

@netojose
Copy link
Owner

netojose commented Jul 6, 2019

@ataylor32 , the comparison for determinate if the field is checked or not is made using ===.
So 1 !== '1' or true !== '1'

You should use the same values and types, for this check work as expected.
Suggestions:

  1. Make sure you are using the same value/types for name/value fields
  2. Format you model output, using Laravel accessors:
// Post model:
...
getAllowCommentsAttribute($value){
   return $value ? '1' : null;
}
...
  1. Convert model to array, and edit specific value:
// on Controller:
$data = $post->toArray();
$data['allow_comments'] = $data['allow_comments'] ? '1' : null;

// on View:
{!! Form::open()->fill($data) !!}
  1. Check the value inline, using the checkbox method fourth parameter:
{!! Form::checkbox('allow_comments', 'Allow comments?', '1', $post->allow_comments)!!}
  1. Check the value inline, using the checked method:
{!! Form::checkbox('allow_comments', 'Allow comments?', '1')->checked($post->allow_comments)!!}

@netojose netojose closed this as completed Jul 6, 2019
@netojose netojose added the invalid This doesn't seem right label Jul 6, 2019
@ataylor32
Copy link
Contributor Author

Thanks for your time and input on this!

Since the allow_comments field is a boolean field, Laravel will have its value set to 0 or 1 (in both cases, it's an integer, not a string). So I thought I could fix this by changing the third parameter of Form::checkbox('allow_comments', 'Allow comments?', '1') from '1' (a string) to 1 (an integer), which seems to be what your first suggestion is, but the checkbox remained unchecked. So I tried your fourth and fifth suggestions, but the checkbox remained unchecked for both of those suggestions too. I didn't try your third suggestion. Finally, I tried your second suggestion, and that's what worked. This is what I used:

public function getAllowCommentsAttribute($value) {
   return $value ? '1' : '0';
}

With this approach, it's the same values as what Laravel uses by default, only as a string ('0' or '1') instead of an integer (0 or 1).

My question is: why didn't changing the third parameter of Form::checkbox('allow_comments', 'Allow comments?', '1') from '1' (a string) to 1 (an integer) work? Laravel has the model instance's allow_comments set to 1 (an integer), so shouldn't the checkbox have been checked? Is that a bug? I have a fair amount of boolean fields in the forms in my project and I'd rather not set up accessors for each of them if it can be avoided.

Thanks again for your help!

@netojose
Copy link
Owner

netojose commented Jul 7, 2019

@ataylor32 you are right, no make sense change model attributes only for work a form field (is too aggressive).

I will make additional tests here (create a database, with a table, with a boolean field and try to simulate the same scenario). In my opinion, since Laravel knows this field is a boolean, maybe the Laravel apply some cast and already there is an internal accessor, and return true/false value.

I will make some tests to see the results, but I think when the field was a checkbox/radio, I can change the verification, using another approach instead of ===.

@netojose netojose reopened this Jul 7, 2019
@netojose netojose self-assigned this Jul 7, 2019
@netojose netojose added bug Something isn't working and removed invalid This doesn't seem right labels Jul 7, 2019
@ataylor32
Copy link
Contributor Author

Do you have a guess as to when you might have a chance to work on this? It's fine if you don't, but I thought I'd ask. Also, would it help if I created a simple project demonstrating the bug?

@kaziu687
Copy link

Bug seems to exists in that line of code

$isChecked = isset($formData[$name]) ? $formData[$name] === $value : $checked;

$formData[$name] (true) is compared with $value ("on")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants