2

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

1

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Hi Dirk. Actually I have no methode called setFilterCheckbox. Are you suggesting me to create one in ActivityFilterType ('cause I have no ActivityFilter class neither) ?
You can also just use an array as $activityFilter. $activityFilter = array('filter_checkbox' => 1). It might be that you need Symfony 2.1 for this, instead of 2.0. But I'm not sure. I normally use entities for my forms to store the valies in a database.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.