【问题标题】:How to bind data to the form in symfony如何在 symfony 中将数据绑定到表单
【发布时间】:2013-04-22 08:49:05
【问题描述】:

我有一个变量需要在 symfony 中绑定到表单中,如何将动作文件中的数据绑定到表单中?

动作文件 $this->unitCost = $this->getUser()->getAttribute('unit_cost'); $value_lists = ($this->unitCost);

    foreach($value_lists as $values)
        {
        echo $values['unit_cost'];
        }
    $form = new MyForm(); $form->bind(array('unit_cost' => $values['unit_cost']));
     //print_r($value_lists);

表格 公共函数配置(){

    $cost_range[''] = '-- Please select --';
    for ($i = 0; ($i <= 100); $i++) {
        $cost_range[$i] = $i;
    }

    $this->setWidgets(array(
        'user_id' => new sfWidgetFormDoctrineChoice(array('model' => 'Person', 'add_empty' => '-- Please select --'), array('onchange' => 'filerUnitCostByName()', 'id' => 'user_id')),
        'unit_cost' => new sfWidgetFormSelect(array('choices' => $cost_range), array('id'=>'unit_cost')),
    ));

如何通过表单下拉数字显示绑定值 我从下拉列表中获取数据并放入会话,然后在操作中检索数据。我想在使用小部件创建的表单中创建的下拉列表中设置选定的变量。正如您在小部件区域中看到的那样。我想将该值添加为默认值。

【问题讨论】:

标签: symfony1 symfony-1.4


【解决方案1】:

对于来自 POST 请求的绑定变量,您可以这样做:

$form = new MyForm();
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

如果你想绑定一个额外的变量,你可以这样做:

$form = new MyForm();
$form->bind(array_merge($request->getParameter($form->getName()), array('foo' => $foo)), $request->getFiles($form->getName()));

编辑:没有 POST 变量,你可以这样做:

$form = new MyForm();
$form->bind(array('foo' => $foo));

【讨论】:

  • 是否可以在没有发布请求的情况下添加绑定。我希望仅在表单中查看详细信息,作为下拉菜单中的一个项目。
  • 是的:$form = new MyForm(); $form->bind(array('foo' => $foo));
猜你喜欢
  • 1970-01-01
  • 2021-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多