开发者

symfony admin generator form object

开发者 https://www.devze.com 2023-01-30 17:22 出处:网络
Hey guys, I\'ve used the Symfony admin generator for a module. Everything is working, but when the form for my model is instantiated, I need to pass in my own option.

Hey guys, I've used the Symfony admin generator for a module.

Everything is working, but when the form for my model is instantiated, I need to pass in my own option.

I could do this myself by overriding the executeNew, executeCreate functions in myModuleActions.class.php (which extends myModuleAutoActions).

But I was hoping for a neater solution?

Perhaps overriding one of the configuration classes is the way to go. I basically need to add the current sf_user object ($this->getUser) as an "sf_user" option for the form, to avoid using sfContext in the myModuleFor开发者_运维知识库m.

Any ideas?


Welcome to Stack Overflow, jolly18.

I would just use sfContext. For example, in my app, I have a subform that creates a new Note object and assigns the user to it. In my form's configure() I have:

$new_note->setAuthor(sfContext::getInstance()->getUser()->getUsername());

I see the book calls this "The fastest but ugly way" because it makes "a big coupling between the form and the context, making the testing and reusability more difficult." But in practice... this works well and I can move on.


if module was generated using admin-generator :

in apps/backend/modules/books/actions/actions.class.php

modify: in

executeEdit(){

//leave rest unchanged

$values=array('activity_id'=>$activity_id, 'book_id'=>$book_id, 'todo_id'=>$todo_id, 'user_id'=>$this->getUser()->getGuardUser()->getId());


    $this->form = new TabelBooksForm($TabelBooks, $values);
}

modify: in

executeNew(){

//leave rest unchanged

$values=array('activity_id'=>$activity_id, 'book_id'=>$book_id, 'todo_id'=>$todo_id, 'user_id'=>$this->getUser()->getGuardUser()->getId());

    $this->form = new TabelBooksForm(array(), $values);
}

in TabelBooksForm.class.php

public function configure()
  {

   if ($this->isNew()) {
    $this->setWidget('book_id', new sfWidgetFormInputHidden());
    $this->setDefault('book_id', $this->getOption('book_id'));    

    $this->setWidget('activity_id', new sfWidgetFormInputHidden());
    $this->setDefault('activity_id', $this->getOption('activity_id'));    

    $this->setWidget('todo_id', new sfWidgetFormInputHidden());
    $this->setDefault('todo_id', $this->getOption('todo_id'));  
  }
}


i've been facing this problem for a while but symfony always surprises me with some neat code that i was not aware of.

I assume you'r using sfPropelPlugin, quite standar, if you checkout the code generated in cache (note: this code will be available once you tried to open the module from the browser, so firts try to look at it so we dont get in trouble :P) you may see something like:

cache/{application_name}(generally frontend or backend)/dev(enviromnemt)/autoModule_name( look here for the module)/:

  • lib
  • action

The action folder contains an action.class.php file that defines all actions generated by the generator (executeNew, Edit, Create, Update, etc). If you look a the implementation of executeNew and executeEdit, you can see that they ask a configuration instace the actual form to display, here is an example:

  public function executeNew(sfWebRequest $request)
  {
    $this->form = $this->configuration->getForm();
    $this->PaymentOrder = $this->form->getObject();
  }

The configuration var containt an instance of a configuration class defined in the lib folder i mentioned earlier. That class tweaks the form to fit the object needs (generally by setting a fresh object instance).

So here comes the magic, the classes you see in your module extend from those in cache, so by pure logic, if you modifi the getForm() method in the main module/lib folder to fit your needs, you wont have to hack forms by getting user valuer where you shouldn't.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号