symfony 重写 BaseForm 类 Howto

发布于 2024-09-08 03:40:20 字数 1640 浏览 6 评论 0原文

我已经安装了 sfDoctrineGuard 插件。一切正常,我可以使用 /sf_guard_user/edit/:id 页面来编辑用户。

我不喜欢将权限列为选择列表的方式,我想将它们显示为根据权限名称拆分的单独复选框。为此,我创建了一个扩展 sfWidgetFormChoice 的自定义小部件。这也按照我想要的方式工作,但我的问题如下:

为了使用我的自定义小部件,我在此文件中编辑了以下行:

lib/form/doctrine/sfDoctrineGuardPlugin/base/BasesfGuardUserForm.class.php

之前:

      'groups_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup')),
      'permissions_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission')),

之后:

      'groups_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true,     'model' => 'sfGuardGroup', 'expanded' => true)),
      'permissions_list' => new myCustomPermissionWidget(),

这给出了正确的结果。

问题是我不应该编辑基类,因为每次构建模型时文件都会被覆盖。

所以我应该编辑这个文件:

lib/form/doctrine/sfDoctrineGuardPlugin/sfGuardUserForm.class.php

    class sfGuardUserForm extends PluginsfGuardUserForm
    {
      public function configure()
      {
        parent::configure();

        $this->setWidgets(array(
          'groups_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup', 'expanded' => true)),
          'permissions_list' => new myCustomPermissionWidget(),
        ));
      }
    }

但这不起作用。我已经尝试了新函数 setup() 中的代码,在我的代码之前和之后使用了parent::setup(),但仍然没有任何结果。

PluginsfGuardUserForm 是抽象的并扩展了 BasesfGuardUserForm 但我不明白为什么这会阻止它工作。

有什么想法吗?

谢谢

I've installed the sfDoctrineGuard plugin. Everything is working, I can use the /sf_guard_user/edit/:id page to edit a user.

I didn't like the way the permissions were listed as a select list, I wanted to display them as individual checkboxes split up based on the permission name. To do this I created a custom widget that extends sfWidgetFormChoice. This is working the way I want it as well, but my problem is the following:

To use my custom widget, I edited the following lines in this file:

lib/form/doctrine/sfDoctrineGuardPlugin/base/BasesfGuardUserForm.class.php

Before:

      'groups_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup')),
      'permissions_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission')),

After:

      'groups_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true,     'model' => 'sfGuardGroup', 'expanded' => true)),
      'permissions_list' => new myCustomPermissionWidget(),

That gives the correct outcome.

The problem is that I shouldn't have edited the Base class as any time I build my model the file is overwritten.

So I should edit this file:

lib/form/doctrine/sfDoctrineGuardPlugin/sfGuardUserForm.class.php

    class sfGuardUserForm extends PluginsfGuardUserForm
    {
      public function configure()
      {
        parent::configure();

        $this->setWidgets(array(
          'groups_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardGroup', 'expanded' => true)),
          'permissions_list' => new myCustomPermissionWidget(),
        ));
      }
    }

But this does not work. I've tried the code inside a new function setup(), with parent::setup() before and after my code but still nothing.

PluginsfGuardUserForm is abstract and extends BasesfGuardUserForm but I don't see why that would stop it from working.

Any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2024-09-15 03:40:20

我相信编辑用户操作使用插件目录中的类 sfGuardUserAdminForm

将文件

plugins/sfDoctrineGuardPlugin/lib/form/doctrine/sfGuardUserAdminForm.class.php

复制到

lib/form/doctrine/

然后将此行添加到 configure()方法

$this->setWidget('permissions_list' => new myCustomPermissionWidget());

您不需要添加对parent::configure() 的调用,在表单框架中执行此操作是不好的做法,并且只有在您知道需要时才应该执行此操作。

I believe the edit user action uses the class sfGuardUserAdminForm which is in the plugin directory

Copy the file

plugins/sfDoctrineGuardPlugin/lib/form/doctrine/sfGuardUserAdminForm.class.php

into

lib/form/doctrine/

Then add this line to the configure() method

$this->setWidget('permissions_list' => new myCustomPermissionWidget());

You do not need to add a call to parent::configure() it is bad practice to do this in the form framework and you should only do it if you know you need to.

咽泪装欢 2024-09-15 03:40:20

尝试编辑

lib/vendor/symfony/lib/plugins/sfDoctrineGuardPlugin/lib/form/doctrine/PluginsfGuardUserForm.class.php

“vendor”和“symfony”将是您安装时安装的任何内容。当我想从登录表单中删除“记住我”复选框时,这对我有用:

<?php

/**
 * sfGuardFormSignin for sfGuardAuth signin action
 *
 * @package    sfDoctrineGuardPlugin
 * @subpackage form
 * @author     Fabien Potencier <[email protected]>
 * @version    SVN: $Id: sfGuardFormSignin.class.php 23536 2009-11-02 21:41:21Z Kris.Wallsmith $
 */
class sfGuardFormSignin extends BasesfGuardFormSignin
{
  /**
   * @see sfForm
   */
  public function configure()
  {
    $this->widgetSchema->setFormFormatterName('list');
    unset($this['remember']);
  }
}

就这么简单。

希望对

卢克有帮助

Try editing the

lib/vendor/symfony/lib/plugins/sfDoctrineGuardPlugin/lib/form/doctrine/PluginsfGuardUserForm.class.php

"vendor" and "symfony" will be whatever you have it as on your install. This worked for me when I wanted to remove the remember me checkbox from the signin form:

<?php

/**
 * sfGuardFormSignin for sfGuardAuth signin action
 *
 * @package    sfDoctrineGuardPlugin
 * @subpackage form
 * @author     Fabien Potencier <[email protected]>
 * @version    SVN: $Id: sfGuardFormSignin.class.php 23536 2009-11-02 21:41:21Z Kris.Wallsmith $
 */
class sfGuardFormSignin extends BasesfGuardFormSignin
{
  /**
   * @see sfForm
   */
  public function configure()
  {
    $this->widgetSchema->setFormFormatterName('list');
    unset($this['remember']);
  }
}

Was as simple as that.

Hope it helps

Luke

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文