0

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

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

<?php echo $form ?> 

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

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

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

现在我有两种形式,一种是基本形式,它只是嵌入了另一种形式。

<?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值不会保存。

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

谢谢

4

2 回答 2

0

调用$this->saveEmbeddedForms()保存labSupportForm方法

于 2011-04-26T16:23:49.737 回答
0

当您按字段呈现表单时,您必须显式调用 $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>

另外,请注意将嵌入的表单名称与关系名称(例如'profile')调用相同的名称,否则保存时会遇到麻烦。只需添加 '_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; ?> 
于 2011-04-26T16:26:04.153 回答