如何在 Symfony 表单选择框中设置所选选项 [英] how to set the selected option in Symfony forms select box

查看:27
本文介绍了如何在 Symfony 表单选择框中设置所选选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 Symfony 表单创建的表单.

I have a form created with Symfony forms.

并且在模板中我有这个选择框,使用渲染方法显示在页面上.

and in the template i have this selectbox, displayed on the page with the render method.

<?php echo $form['field']->render() ?>

是否可以设置这个选择框的选中选项?

is it possible to set the selected option of this select box?

或者这是否必须在创建此表单的类中完成?有字段的创建完成:

Or does this have to be done in the class that creates this form? There is the creation of the field done:

public function configure() {
    $this->widgetSchema['field'] = new sfWidgetFormSelect(
      array("choices" => 
          array('1' => 'test1','2' => 'test2')
      )
    );
  }

推荐答案

是的,当然——你应该设置相应的表单值——要么通过 bind(),要么通过小部件的 default 选项.

yes, sure — you should have set corresponding form value — either via bind(), either via widget's default option.

例如,

public function configure() 
{
    $this->widgetSchema['field'] = new sfWidgetFormSelect(array(
        "choices" => array('1' => 'test1','2' => 'test2'), 
        'default' => 2));
}

希望我已经回答了您的问题.

Hope I've answered your question.

这篇关于如何在 Symfony 表单选择框中设置所选选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆