symfony 重写 BaseForm 类 Howto
我已经安装了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信编辑用户操作使用插件目录中的类 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.
尝试编辑
“vendor”和“symfony”将是您安装时安装的任何内容。当我想从登录表单中删除“记住我”复选框时,这对我有用:
就这么简单。
希望对
卢克有帮助
Try editing the
"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:
Was as simple as that.
Hope it helps
Luke