Question

The title says pretty much everything, but here are pieces of my code to illustrate what I'm trying to do.

Here is the Form Type

//[...]
class ActivityFilterType extends AbstractType
{
  //[...]
  public function buildForm(FormBuilder $builder, array $options)
  {
        $builder->add('filter_checkbox', 'choice', array(
                'label'    => $this->translator->trans('form.label.sexFilter'),
                'choices'  => array(1 => ucfirst($this->translator->transChoice('sex.female', 2)), 2 => ucfirst($this->translator->transChoice('sex.male', 2))),
                'multiple' => true,
                'expanded' => true,
                'required' => false,
                'empty_value' => false,
                /*
                   // those ones didn't make it
                   'attr'     => array( 1=>array('checked' => 'checked'), 2=>array('checked' => 'checked') ),
                   'preferred_choices' => array(1, 2),
                */
        ));
  }
  //[...]
}

And the form Template

<div class="filter-message">
    <div class="select-group">
        {{ form_label(form.filter_dropdown) }}
        <div class="input control-group filter" >
            {{ form_widget(form.filter_dropdown) }}
        </div>
    </div>
    <div class="checkbox">
        {{ form_label(form.filter_checkbox) }}
        {{ form_widget(form.filter_checkbox, {'attr':{'checked':checked}}) }} 
        <!-- that didn't do it neither -->
    </div>
</div>

Thanks

Was it helpful?

Solution

I followed @bentidy answer in a previous post pointed here by @Squazic. It's pretty simple

So for me, as I want both sex checkbox to be checked by default, I had to add the data argument as an array (it accepts both a single value and an array).

It goes like this :

//[...]
class ActivityFilterType extends AbstractType
{
  //[...]
  public function buildForm(FormBuilder $builder, array $options)
  {
    $builder->add('filter_checkbox', 'choice', array(
            'label'    => $this->translator->trans('form.label.sexFilter'),
            'choices'  => array(1 => ucfirst($this->translator->transChoice('sex.female', 2)), 2 => ucfirst($this->translator->transChoice('sex.male', 2))),
            'multiple' => true,
            'expanded' => true,
            'required' => false,
            'empty_value' => false,
            'data' => array(1,2) // female value is 1 and male value is 2
    ));
  }
  //[...]
}

Here is the related doc : http://symfony.com/doc/2.0/reference/forms/types/field.html

Thanks Guys

OTHER TIPS

You do this in the controller:

$activityFilter = new ActivityFilter();
$activityFilter->setFilterCheckbox(1);
$form = $this->createForm(new ActivityFilterType(), $activityFilter);
...

BTW: You shouldn't name your attribute filter_checkbox but rather make it more specific what is stored. For instance sexFilterin this case.

If you have a checkbox array you must set 'data' to an array of the checkboxes values that you want to make checked. Like that:

'data' =>array(0,1,2),'choices'=>array(0=>'Permitem Acesso Livre', 1=>'Exigem Cadastro', 2=>'Cobram Mensalidades ou Taxas')

That's it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top