symfony - 使用embedForm自定义表单和更新记录

时间:2011-04-21 10:12:47

标签: symfony1 symfony-1.4

embedForm使用doctrine和SF1.4

时出现小问题

我创建了一个模块,用户可以使用sfDoctrineGuard编辑/创建/删除特定组中的用户

表单只是一个名为manufacturerForm.class.php的自定义表单,它扩展了sfGuardUserAdminForm

class manaufacturerForm extends sfGuardUserAdminForm
{
 public function configure()
 {
     $form = new sfGuardUserProfileForm();
     $this->embedForm('profile', $form);

     unset($this['firstname'], //this field is in sfGuardUserProfile is shown in form
           $this['user_id'],   //this field is in sfGuardUserProfile is shown in form
           $this['is_super_admin'], //this field is in sfGuardUser is not shown
           $this['is_admin'], // this filed is in sfGuardUser is not shown
           $this['permissions_list'], //this field is in sfGuardUser is not shown
           $this['groups_list']);//this field is in sfGuardUser is not shown
  }
}

您可以看到我在sfGuardUserProfileForm内嵌入了manufacturerForm

我有2个问题,sfGuardUserProfile包含我不想为此特定表单显示的字段,例如: firstname, lastname, email_new, created_at, updated_at,但我似乎无法取消它们,因为它们仍会显示。

请注意,此管理模块没有generator.yml文件。我正在editSuccess.php中使用_form.php部分来完成所有这些操作。

我的另一个问题是,当我编辑现有用户并保存一切都很好。当我尝试编辑时出现问题,因为我得到以下内容:

An object with the same "user_id" already exist. - 这是针对sfGuardUserProfile中的user_id字段,email_new字段也是如此。我得到An object with the same "email_new" already exist.

1)如何取消设置我不需要的字段?

2)我是否需要覆盖任何doSave(),updateObject(),saveEmbeddedForms()方法来停止上一个问题?

由于

1 个答案:

答案 0 :(得分:1)

要从嵌入表单中删除字段,您需要在sfGuardUserProfileForm表单中执行此操作。这是因为字段只有在嵌入manufacturerForm时才会被读取。

如果您需要从通常不会执行的sfGuardUserProfileForm中删除自定义字段,只需扩展该类,删除其中的字段并嵌入新的扩展表单。

当您嵌入表单时,需要传入现有的sfGuardUserPofile对象。

class manaufacturerForm extends sfGuardUserAdminForm
{
    public function configure()
    {
        $form = new manaufacturerProfileForm($this->getObject()->getProfile());
        $this->embedForm('profile', $form);
    }
}


class manaufacturerProfileForm extends sfGuardUserProfileForm
{
    public function configure()
    {
        unset($this['firstname'], //this field is in sfGuardUserProfile is shown in form
              $this['user_id'],   //this field is in sfGuardUserProfile is shown in form
              $this['is_super_admin'], //this field is in sfGuardUser is not shown
              $this['is_admin'], // this filed is in sfGuardUser is not shown
              $this['permissions_list'], //this field is in sfGuardUser is not shown
              $this['groups_list']);//this field is in sfGuardUser is not shown
     }
相关问题