mardi 29 septembre 2015

Checkbox checks even if 'false' provided from DTO

Having form like:

//defaults
$this->defaults = [
    'label' => false,
    'attr'  => [
        'autocomplete' => 'off',
    ],
];

//part of BuildForm()
->add('send_reminders', new CheckboxType, $this->defaults + [
    'required' => false,
    'data' => false,
])

DTO like:

(other fields)

/** @var bool */
pivate $sendReminders = true;

(getters and setters)

and custom form view like:

<div class="form-group">
    <label class="accept-terms">
        <input type="checkbox" id="{{ form.send_reminders.vars.id }}" name="{{ form.send_reminders.vars.full_name }}" {{ form.send_reminders.vars.value == 1 ? 'checked' : '' }}/>
        &nbsp;{{ 'visit_booked_phone_confirmation'|trans }}
    </label>
</div>

I use above for Foo entity creation and edition, by hydrating DTO.

I have following problem: While in entity creation, checkbox works just fine - it is checked by default, if user unchecks it, false is provided to DTO. If there are form errors from other fields and form is reloaded, checkbox remains unchecked.

While in entity edition, even if there's false in DTO, checkbox comes checked. I thought that I have problem with my custom form rendering twig, but I checked checkbox value:

{{ dump(form.send_reminders.vars.value) }} //returns "1"

I tried setting various attributes to form field, like required, data, empty_data and so on, nothing works. What am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire