Symfony Form Default Checked Checkbox List

How to pre check check boxes in a Symfony form

This form contains multiple checkboxes, with preselected values. For the sake of simplicity I've chosen static data, but it could be implemented easily in a dynamic way, or with a Doctrine query. Here you can see a small list with well know cities of the world, I want to check by default Paris and New York, just look at this simple code in your form builder:

        // Choices list data
        $choicesData = array(
            0 => 'Paris',
            1 => 'London',
            3 => 'Rome',
            4 => 'New York',
            5 => 'Madrid',

        // Check these two by default
        $selected = array(
            'Paris' => 0,
            'New York' => 4

        $builder->add('cities', 'choice', array(
            'choices' => array(
            'multiple' => true,
            'expanded' => true,
            'data' => $selected

As you can see preselected values are an array flip with selected data, that is, inversing the array index with the checkbox name and the value with the checkbox index.

Of course if the field were linked to an array collection related to the entity, the existing values will be checked by default.