php – Symfony2:在单选按钮选择表单中设置数据库的默认值?

symfony2的新手,我有一个包含2个字段的简单表.

由于警报字段是一个布尔值,我声明了这样的形式:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder                                
        ->add('message', 'text', array('label' => "Message"))                
        ->add('alert', 'choice', array(
              'choices'   => array(1 => 'Yes', 0 => 'No'),
              'expanded'  => true,
              'multiple'  => false,
              'label'     => "Are you agree?",
              'attr'      => array('class' => 'well')
        ));
}

它在我创建新条目时有效,但是当我尝试编辑条目时,存储在数据库中的“警报”选项未在表单中设置(单选按钮).

如何在表单中设置字段的数据库状态?

解决方法:

你有两个选择.

尝试使用formbuilder中的data属性.

$builder                                
        ->add('message', 'text', array('label' => "Message"))                
        ->add('alert', 'choice', array(
              'choices'   => array(1 => 'Yes', 0 => 'No'),
              'expanded'  => true,
              'multiple'  => false,
              'label'     => "Are you agree?",
              'data'      => $entity->getAlert(),
              'attr'      => array('class' => 'well')
        ));

要么:
在symfony中创建表单时,通常会将数据实体传递给该表单.此自动填充所有值.

$this->createForm(new FormType(), $entity);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...