Search code examples
formssymfony1symfony-formssfguardsfdoctrineguard

symfony override BaseForm class howto


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.