Duddy67
Duddy67

Reputation: 1046

Laravel: keep the checkbox selection after a validation error?

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 ?

Upvotes: 0

Views: 777

Answers (1)

Kaushik Dhameliya
Kaushik Dhameliya

Reputation: 191

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' : '' }}> 

Upvotes: 1

Related Questions