Santiago Capdevila
Santiago Capdevila

Reputation: 125

Get checkbox checked after laravel validation error

I need to keep the checked checkboxes in the form as checked after I get the validation errors in the Controller.

Actually I find quite horrendous my implementation of the checkboxes in my form.

Could you please provide some advise? Thanks

<div class="form-group">
                {!! Form::label('call', 'Llamar:', ['class' => 'control-label']) !!}
                {!! Form::checkbox('call', isset($category)?(bool)$category->call:true, ['class' => 'form-control']) !!}
            </div>

Upvotes: 0

Views: 1818

Answers (2)

pdolinaj
pdolinaj

Reputation: 1145

You can do it via the form builder much easier like this:

{!! Form::checkbox('call', 1, false, ['class' => 'form-control']) !!}
  • call = your checkbox name
  • 1 = your checkbox value if it's checked (change to whatever you want)
  • false = don't load it checked on first page load (setting true will load it always checked)
  • ['class' => 'form-control'] = it will add the class to it

That's all.

You don't need to worry about any kind of values, it will be set automatically.

Upvotes: 0

nsg
nsg

Reputation: 129

For the checkbox part, I did not use Laravel Forms. I used normal html "input" tag instead, and used "old" to check for previous value

<input type="checkbox" name="call" value="1" {{(old('call') == "1") ? 'checked': ''}}>Call Try using old to check the previous value and set the value to checked or not based on that.

Upvotes: 1

Related Questions