phpsymfony1symfony-1.4

Admin generator - Using different forms for "edit" and "new" actions


How do I configure generator.yml so that it will use one form for the "new" action, and another for the "edit" action ?


Solution

  • Two ways:

    1. Write your own admin configuration

    This is the preferred method if there are significant differences between the two forms. This file goes in the config folder of the module. "moduleName" should be replaced with the name of the module.

    class moduleNameGeneratorConfiguration extends BaseModuleNameGeneratorConfiguration
    { 
      public function getForm($object = null, $options = array())
      {
        $options = array_merge($this->getFormOptions(), $options);
        if ($object && $object->exists())
        {
          return new EditModelForm($object, $options);
        }
        else
        {
          return new NewModelForm($object, $options);          
        }
      }
    }
    

    2. Separate the logic inside of the form

    This can get messy if there are significant differences, but you can simply call isNew inside of the form as necessary, e.g.

    public function configure()
    {
      if ($this->isNew())
      {
        //do new stuff
      }
      else
      {
        //do edit stuff
      }
    }