选择菜单默认值不在symfony中呈现

时间:2011-01-04 11:31:07

标签: forms symfony1 widget

我正在使用Symfony 1.4,并且使用嵌入表单将多个相似的表单放入一个配置页面的表单中。我正在成功显示表单,但sfWidgetFormChoice窗口小部件的默认值未呈现,即selected =“selected”属性已从HTML中消失。

顺便提一下,如果我不使用嵌入表单,则会显示默认值。避免嵌入表单的问题是每个表单都有相同的输入,因此会覆盖自己。

动作代码是这样的,为简洁起见省略了一些代码:

  $serviceFormArray = array();
  $this->fullForm = new ConfigForm();

  foreach($this->serviceArray as $net => $service)
  {

    $this->partialForm = new ConfigForm();

    foreach($service as $typeId => $val)
    {
      $typeObj = Doctrine::getTable('Type')->find($typeId); 
      $typeField = new sfWidgetFormChoice(array(
          'default' => $val,
          'choices' => array('1' => 'on', '0' => 'off'),
          'label' => $typeObj->name)
          );

      $typeField->setDefault($val);

      $serviceFormArray[$typeObj->name] = $typeField;
    }
    $netObj = Doctrine::getTable('Network')->find($net);
    $this->partialForm->setWidgets($serviceFormArray);
    $this->fullForm->embedForm($netObj->name,$this->partialForm);

  }

,模板看起来像这样,为简洁起见省略了一些代码:

<div class="sectionBox">
  <?php echo $fullForm->renderFormTag('/configure/submitconfig') ?>
    <?php foreach ($fullForm->getVisibleFields() as $part => $field): ?>
    <div class="settingsField">
      <?php echo $field->renderLabel() ?>
      <?php echo $field->render() ?>
      <input type="hidden" name="plug" value="<?php echo $plugName; ?>"/>
    </div>
    <?php endforeach; ?>
    <div id="submitConfig"><input type="submit" value="Save"/></div>
  </form>
</div>

1 个答案:

答案 0 :(得分:3)

尝试通过$form->setDefault($name, $default)设置默认值。

$this->partialForm->setDefault($typeObj->name, $val);