1

Using Laravel 5.1

I have a form that I am data binding to. Everything work as expected except for the checkbox.

Here is my input (blade template);

<input type="checkbox" name="noContact" value="{{ $profile['noContact'] }}">

The field is a boolean and the checked value should be 1. Doesn't seem to be binding. What am I missing?

Thanks!

3 Answers 3

3

Maybe try to use checked="checked" instead of value attribute :

<input type="checkbox" name="noContact"{{ $profile['noContact'] ? ' checked="checked"' : '' }}>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Soywod! That did it.
2

In your example if you set the value to "bla bla" and the checkbox is checked, when you send the form in the controller you will get exactly "bla bla".

If you want only to check if the checkbox is checked or not, just set the value to 1 and in the controller handle it like this:

$checked = (bool)Input::get("checkbox_name");

Or in a condition:

if(!empty(Input::get("checkbox_name"))){...}

1 Comment

Thanks Claudio. What I am trying to do is present a form to edit the users profile. If they have 'noContact' checked in their profile, then it should be checked when the edit form is presented. Can't seem to bind that value...
0

In laravel, you can try to use Forms & HTML.

Form::checkbox( "noContact", $profile['noContact']);

1 Comment

Thanks for the reply. Actually I chose not to use the Form facade because oddly, you can't add any html attributes (easily) to the label. Like * for mandatory fields for example. Otherwise, yes the above is a very easy way to achieve what I was after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.