symfony override BaseForm class howto
-
02-10-2019 - |
Question
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
Solution
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.
OTHER TIPS
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 <fabien.potencier@symfony-project.com>
* @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