如何使用symfony2表单生成器呈现默认选中的复选框?

时间:2022-04-22 06:45:40

I have not found any easy way to accomplish to simply check a Checkbox by default. That can not be that hard, so what am i missing?

我没有找到任何简单的方法来完成,默认情况下只需检查一个复选框。那可不是那么难,所以我错过了什么?

11 个解决方案

#1


16  

You would simply set the value in your model or entity to true and than pass it to the FormBuilder then it should be checked.

您只需将模型或实体中的值设置为true,然后将其传递给FormBuilder,然后检查它。

If you have a look at the first example in the documentation:

如果您查看文档中的第一个示例:

A new task is created, then setTask is executed and this task is added to the FormBuilder. If you do the same thing with your checkbox

创建一个新任务,然后执行setTask并将此任务添加到FormBuilder。如果您对复选框执行相同操作

$object->setCheckboxValue(true);

and pass the object you should see the checkbox checked.

并传递对象,您应该看到复选框已选中。

If it's not working as expected, please get back with some sample code reproducing the error.

如果它没有按预期工作,请返回一些重现错误的示例代码。

#2


35  

You can also just set the attr attribute in the form builder buildForm method:

您也可以在表单构建器buildForm方法中设置attr属性:

$builder->add('isPublic', 'checkbox', array(
    'attr' => array('checked'   => 'checked'),
));

#3


18  

In Symfony >= 2.3 "property_path" became "mapped".

在Symfony> = 2.3中,“property_path”变为“已映射”。

So:

所以:

$builder->add('checkboxName', 'checkbox', array('mapped' => false,
    'label' => 'customLabel',
    'data' => true, // Default checked
));

#4


15  

Setting the 'data' option works for me. I'm creating a non entity based form:

设置'data'选项对我有用。我正在创建一个非实体形式:

$builder->add('isRated','checkbox', array(
    'data' => true
));

#5


8  

In TWIG

If you wish to do this in the template directly:

如果您希望直接在模板中执行此操作:

{{ form_widget(form.fieldName, { 'attr': {'checked': 'checked'} }) }}

#6


4  

Use the FormBuilder::setData() method :

使用FormBuilder :: setData()方法:

$builder->add('fieldName', 'checkbox', array('property_path' => false));
$builder->get('fieldName')->setData( true );

"property_path" to false cause this is a non-entity field (Otherwise you should set the default value to true using your entity setter).

“property_path”为false导致这是一个非实体字段(否则,您应该使用实体设置器将默认值设置为true)。

Checkbox will be checked by default.

默认情况下将选中复选框。

#7


1  

This works as well, but aware of persistent "checked" state

这也有效,但意识到持久的“检查”状态

$builder->add('isPublic', 'checkbox', array(
    'empty_data' => 'on',
));

#8


1  

You should make changes to temporary object where entity is stored before displaying it on form. Something like next:

在表单上显示实体之前,您应该对存储实体的临时对象进行更改。像下一个:

<?php

namespace KPI\AnnouncementsBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class AnnouncementType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {  
        // ...        

        if ($options['data']->getDisplayed() === null) {
            $options['data']->setDisplayed(true);
        }

        // ...

        $builder
            ->add('displayed', 'checkbox', array(
                'required' => false
            ));
    }
}

#9


1  

As per documentation: http://symfony.com/doc/current/reference/forms/types/checkbox.html#value

根据文档:http://symfony.com/doc/current/reference/forms/types/checkbox.html#value

To make a checkbox or radio button checked by default, use the data option.

要默认选中复选框或单选按钮,请使用数据选项。

#10


0  

UserBundle\Entity\User

let's assume that you have an entity called ( User ) and it has a member named isActive, You can set the checkbox to be checked by default by setting up isActive to true:

假设您有一个名为(User)的实体,并且它有一个名为isActive的成员,您可以通过将isActive设置为true来默认设置复选框:

$user = new User();

// This will set the checkbox to be checked by default
$user->setIsActive(true);

// Create the user data entry form
$form = $this->createForm(new UserType(), $user);

#11


0  

To complete a previous answer, with a multiple field you can do that to check all choices :

要完成上一个答案,使用多个字段可以检查所有选项:

   'choice_attr' => function ($val, $key, $index) {
       return array('checked' => true);
   }

https://symfony.com/doc/3.3/reference/forms/types/choice.html#choice-attr

https://symfony.com/doc/3.3/reference/forms/types/choice.html#choice-attr

#1


16  

You would simply set the value in your model or entity to true and than pass it to the FormBuilder then it should be checked.

您只需将模型或实体中的值设置为true,然后将其传递给FormBuilder,然后检查它。

If you have a look at the first example in the documentation:

如果您查看文档中的第一个示例:

A new task is created, then setTask is executed and this task is added to the FormBuilder. If you do the same thing with your checkbox

创建一个新任务,然后执行setTask并将此任务添加到FormBuilder。如果您对复选框执行相同操作

$object->setCheckboxValue(true);

and pass the object you should see the checkbox checked.

并传递对象,您应该看到复选框已选中。

If it's not working as expected, please get back with some sample code reproducing the error.

如果它没有按预期工作,请返回一些重现错误的示例代码。

#2


35  

You can also just set the attr attribute in the form builder buildForm method:

您也可以在表单构建器buildForm方法中设置attr属性:

$builder->add('isPublic', 'checkbox', array(
    'attr' => array('checked'   => 'checked'),
));

#3


18  

In Symfony >= 2.3 "property_path" became "mapped".

在Symfony> = 2.3中,“property_path”变为“已映射”。

So:

所以:

$builder->add('checkboxName', 'checkbox', array('mapped' => false,
    'label' => 'customLabel',
    'data' => true, // Default checked
));

#4


15  

Setting the 'data' option works for me. I'm creating a non entity based form:

设置'data'选项对我有用。我正在创建一个非实体形式:

$builder->add('isRated','checkbox', array(
    'data' => true
));

#5


8  

In TWIG

If you wish to do this in the template directly:

如果您希望直接在模板中执行此操作:

{{ form_widget(form.fieldName, { 'attr': {'checked': 'checked'} }) }}

#6


4  

Use the FormBuilder::setData() method :

使用FormBuilder :: setData()方法:

$builder->add('fieldName', 'checkbox', array('property_path' => false));
$builder->get('fieldName')->setData( true );

"property_path" to false cause this is a non-entity field (Otherwise you should set the default value to true using your entity setter).

“property_path”为false导致这是一个非实体字段(否则,您应该使用实体设置器将默认值设置为true)。

Checkbox will be checked by default.

默认情况下将选中复选框。

#7


1  

This works as well, but aware of persistent "checked" state

这也有效,但意识到持久的“检查”状态

$builder->add('isPublic', 'checkbox', array(
    'empty_data' => 'on',
));

#8


1  

You should make changes to temporary object where entity is stored before displaying it on form. Something like next:

在表单上显示实体之前,您应该对存储实体的临时对象进行更改。像下一个:

<?php

namespace KPI\AnnouncementsBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class AnnouncementType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {  
        // ...        

        if ($options['data']->getDisplayed() === null) {
            $options['data']->setDisplayed(true);
        }

        // ...

        $builder
            ->add('displayed', 'checkbox', array(
                'required' => false
            ));
    }
}

#9


1  

As per documentation: http://symfony.com/doc/current/reference/forms/types/checkbox.html#value

根据文档:http://symfony.com/doc/current/reference/forms/types/checkbox.html#value

To make a checkbox or radio button checked by default, use the data option.

要默认选中复选框或单选按钮,请使用数据选项。

#10


0  

UserBundle\Entity\User

let's assume that you have an entity called ( User ) and it has a member named isActive, You can set the checkbox to be checked by default by setting up isActive to true:

假设您有一个名为(User)的实体,并且它有一个名为isActive的成员,您可以通过将isActive设置为true来默认设置复选框:

$user = new User();

// This will set the checkbox to be checked by default
$user->setIsActive(true);

// Create the user data entry form
$form = $this->createForm(new UserType(), $user);

#11


0  

To complete a previous answer, with a multiple field you can do that to check all choices :

要完成上一个答案,使用多个字段可以检查所有选项:

   'choice_attr' => function ($val, $key, $index) {
       return array('checked' => true);
   }

https://symfony.com/doc/3.3/reference/forms/types/choice.html#choice-attr

https://symfony.com/doc/3.3/reference/forms/types/choice.html#choice-attr