如何在symfony中将数据绑定到表单

问题描述 投票:0回答:1

我有一个变量需要绑定到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')),
    ));

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

php symfony1 symfony-1.4
1个回答
2
投票

对于 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));
© www.soinside.com 2019 - 2024. All rights reserved.