加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

表单 – 如何使用Symfony 2预先选择表单单选项?

发布时间:2020-12-14 18:48:11 所属栏目:资源 来源:网络整理
导读:我正在编写语言选择表格: $currentLocale = "en_US"; // This is indeed sent to the formType $langs = array( 'fr_FR' = 'fr','en_US' = 'en' ); $builder-add('language','language',array( 'choices' = $langs,'expanded' = true,'multiple' = false,'r
我正在编写语言选择表格:
$currentLocale = "en_US"; // This is indeed sent to the formType

    $langs = array(
        'fr_FR' => 'fr','en_US' => 'en'
        );

    $builder->add('language','language',array(
        'choices' => $langs,'expanded' => true,'multiple' => false,'required' => false,'label' => false,));

HTML代码看起来像这样(简化):

<div id="languageForm_language">
    <input type="radio" value="fr_FR">
    <input type="radio" value="en_US">
</div>

如何根据$currentLocale值预先选择第二项?

解决方法

在$langs数组中,您可以指定键值对,如下所示:
array(
  0 => 'value1',1 => 'value2'
)

现在,例如如果要预先选择value2,可以将data属性设置为value2中的键:

$builder->add('language','choice',array(
    'choices' => $langs,'data' => 1
));

根据这个,您可以将数据属性设置为$currentLocale变量以预先选择它.您的代码应如下所示:

$currentLocale = "en_US"; // This is indeed sent to the formType

$langs = array(
    'fr_FR' => 'fr','en_US' => 'en'
);

$builder->add('language','data' => $currentLocale
));

注意:add()方法的第二个参数应该是选择而不是语言.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读