【问题标题】:symfony - embedForm and form widgets not savingsymfony - embedForm 和表单小部件不保存
【发布时间】:2011-04-26 15:35:56
【问题描述】:

我有一个 EmbeddedForm,我正在尝试为其配置小部件。

目前我只是在_form.php 模板中输出表单,例如:

<?php echo $form ?> 

这很好,但我希望我的表单字段按特定顺序排列,所以我想我会尝试:

<?php echo $form['firstname']->renderRow() ?> 
<?php echo $form['lastname']->renderRow() ?> 
<?php echo $form['email_address']->renderRow() ?> 

这给了我一个无效的小部件错误。

现在我有 2 个表单,一个是基本表单,它只是嵌入了另一个表单。

<?php
class labSupportForm extends sfGuardUserAdminForm
{
   public function configure()
   {
     $form = new labSupportProfileForm($this->getObject()->getProfile());
     $this->embedForm('profile', $form);

   unset($this['is_super_admin'], $this['is_admin'], $this['permissions_list'], $this['groups_list']);

   $this->widgetSchema['profile'] = $form->getWidgetSchema();
   $this->validatorSchema['profile'] = $form->getValidatorSchema();
 }

 public function save($con = null)
 {
   $user = parent::save($con);
   if (!$user->hasGroup('Lab Support'))
   {
     $user->addGroupByName('Lab Support');
     $user->save();
   }
   return $user;
 }
}

和:

<?php
class labSupportProfileForm extends sfGuardUserProfileForm
{
  public function configure()
  {
    unset($this['email_new'],
          $this['validate_at'],
          $this['validate'],
          $this['address_1'],
          $this['address_2'],
          $this['city'],
          $this['country'],
          $this['postcode'],
          $this['created_at'],
          $this['updated_at'],  
          $this['user_id'],   
          $this['is_super_admin'], 
          $this['is_admin'], 
          $this['permissions_list'], 
          $this['groups_list']);
   }
}

但如果我将小部件/验证器添加到 labSupportForm 并保存,firstname 值不会保存。

我在这里做错了吗,因为我认为这个值会节省。

谢谢

【问题讨论】:

  • 你不必写 $this->widgetSchema['profile'] = $form->getWidgetSchema(),这由 embedForm 方法自动处理,验证器模式也是如此。

标签: symfony1 symfony-1.4 symfony-forms


【解决方案1】:

当您按字段呈现表单时,您必须显式调用 $form->renderHiddenFields()。例如:

<?php echo form_tag_for($form, '@url') ?>
  <table>
    <tfoot>
      <tr>
        <td colspan="2">
          <input type="submit" value="Save" />
          <?php echo $form->renderHiddenFields() ?>
        </td>
      </tr>
    </tfoot>
    <tbody>
      <?php echo $form['username']->renderRow() ?>
      <?php echo $form['profile_form']->renderRow() ?>
    </tbody>
  </table>
</form>

另外,请注意调用嵌入表单名称与关系名称相同(例如“配置文件”),否则保存时您将拥有troubles。只需添加'_form'后缀,您就安全了:

$this->embedForm('profile_form', $form);

如果您想保持表单字段的线性显示结构,您应该根据您的小部件架构显式呈现它们:

      <?php echo $form['username']->renderRow() ?>
      <?php echo $form['profile_form']['first_name']->renderRow() ?>
      <?php echo $form['profile_form']['last_name']->renderRow() ?>

或者您可以为嵌入表单的所有字段自动执行此操作:

    <?php foreach ($form['profile_form'] as $field): ?>
      <?php if (!$field->isHidden()): ?>
        <?php echo $field->renderRow() ?>
      <?php endif; ?>
    <?php endforeach; ?> 

【讨论】:

    【解决方案2】:

    labSupportForm保存方法中调用$this-&gt;saveEmbeddedForms()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多