7

How can I add a save and back to list button in the view for creating new record? In general, I want more buttons so I'm looking for a generic answer.

EDIT: Default generator provides me with two buttons:

  1. save
  2. save and add

I want a button that saves and takes me back to the list. And I also want a custom button with a custom action.

Let me explain the actual situation:

I have a list action create that takes me to a form having some input fields. Now I want to make an image using these fields' data. I want to display this image as preview on the right side of the form. For that I need a preview button so that whenever pressed, it submits the data to itself for preview and further modification.

I hope it's clearer now.

prongs
  • 9,422
  • 21
  • 67
  • 105
  • Could your explain better your question? **Save** and **Back to list** button are enabled by default in a form. Where do you want insert more buttons? Do you need also buttons with custom action? – macgyver Dec 08 '11 at 16:49

1 Answers1

8

You have first to add in generator.yml your save_and_back_to_list and other custom my_other_action actions:

    config:
      actions: ~
      fields:  ~
      list:    ~
      filter:  ~
      form:    ~
      edit:
        actions:
          _save: ~
          _list: ~
          save_and_back_to_list: ~
          my_other_action: ~
      new:
        actions:
          _save: ~
          _save_and_add: ~
          _list: ~
          save_and_back_to_list: ~
          my_other_action: ~

Then you have to add your customized helper functions to create your buttons in /apps/myapplication/modules/mymodule/lib/mymoduleGeneratorHelper.class.php

  public function linkToSaveAndBackToList($object, $params)
  {
    return '<li class="sf_admin_action_save_and_back_to_list"><input type="submit" value="'.__($params['label'], array(), 'sf_admin').'" name="save_and_back_to_list" /></li>';
  }

  public function linkToMyOtherAction($object, $params)
  {
    return '<li class="sf_admin_action_my_other_action"><input type="submit" value="'.__($params['label'], array(), 'sf_admin').'" name="my_other_action" /></li>';
  }

Now you get two submit buttons both in New and Edit form.

Finally you have to override the function processForm(sfWebRequest $request, sfForm $form), that you can find in /cache/dev/modules/autoMymodule/actions/actions.class.php, to manage your new submit actions. I guess your create function overrides the admin generator one.

I hope I don't forget nothing.

macgyver
  • 1,279
  • 2
  • 9
  • 16
  • actually my `create` is not overriding the default one, I'll change the name to something else. but I got the idea. – prongs Dec 09 '11 at 08:13
  • The actions provided by the admin generator starts with an underscore, but not in action class. I guess that this Symfony rule has as purpose to get a separation from user actions. – macgyver Dec 09 '11 at 08:32
  • I thought I should make it a separate question. My actual situation: http://stackoverflow.com/questions/8445441/symfony-alternate-new-action – prongs Dec 09 '11 at 12:35
  • Also, When I looked up the source of `/mymodule/new`, I saw the submit action of the form is `/mymodule` instead of `/mymodule/create` as in the _Jobeet_ tutorial. If it is so, then how does it end up calling `executeCreate`? – prongs Dec 09 '11 at 12:41
  • Good question! I guess that the routing system switches to the action create for post request if the form action is not specified. I don't know the reason for this choice. – macgyver Dec 10 '11 at 09:22
  • Also about your new question, I'm dealing with this matter (think I'm developing an extension of the admin generator as plugin), both image upload and admin forms customization, and I will answer hoping and trying to well focus what you need. – macgyver Dec 10 '11 at 09:37
  • well, now I understand how does it end up calling executeCreate. The output of `./symfony app:route backend` tells me that for url `/poster/`, a `GET` or a `HEAD` request corresponds to `index` action and a `POST` request corresponds to `create` action. Weird though. – prongs Dec 10 '11 at 12:54