Search code examples
laravelcheckboxlaravel-5laravelcollective

Form model binding in Laravelcollective, a default checked checkbox never shows unchecked


Laravel 5.5, Laravelcollective 5.4

On my edit page the checkbox is always showing checked, regardless of state in database. (Have confirmed it's working as intended without default checked.)

I use the same form fields for my create route, and would like the default to be checked.

 {{ Form::model($client, ['route' => ['clients.update', $client->id], 'method' => 'patch']) }}
 {{ Form::checkbox('active', 'Yes', true) }}
 {{ Form::close() }}

Solution

  • According to the documentation on form model binding

    So, the priority looks like this:

    1. Session Flash Data (Old Input)
    2. Explicitly Passed Value
    3. Model Attribute Data

    Note that the explicitly Passed Value seems to not be a default fallback but the actual value that will be used if provided (regardless of model). Therefore that seems to be the intended behaviour.

    Update: As discussed in the comments, a solution to allow fallback values in the case where there's no model set is to use:

    {{ Form::checkbox('active', 'Yes', isset($client)?null:true) }}