add edit button to dropdown field, maybe a new widget class

This is for discussion, mainly on 1.x but there's some 2.x topics here too.

Moderators: dwhittle, Ian

add edit button to dropdown field, maybe a new widget class

Postby christian. » Thu Jun 09, 2011 1:15 pm

I am using symfony 1.4 with propel as a backend and have the following use-case:

In my form (generated with symfony propel:generate...) is a dropbox (of class sfWidgetFormPropelChoice) from where the user can select an address (any record from 'address' table). Let's say the selected address has the id '4'. I would like the user to be able to press an edit button right next to the dropbox and a he/she is redirected to 'address/edit?id=4'.

Best case would be that in this form the 'save' button is replaced by 'save and return', but that is a problem which is to be solved later.

I assume that I have to create a new widget class subclassing sfWidgetFormPropelChoice for that -- since I want to do that again in other forms it seems to make sense. I thought about overriding the render() method in the subclass and add a simple submit button. Then I would have to touch the create and update actions and change them to look if this button was pressed instead of the save button, and if it was pressed do the redirection. That seems a little odd to me, because create and update should -- for the sake of clean code -- just create or update ... still there is a method processForm() which is called by both actions which could be the right place.

I could use JavaScript to redirect, but I would prefer a JavaScript free solution.

Anyone another idea? Maybe I could use a facility already provided by symfony which I don't know yet. Or something comparable was already done where I can learn a lesson or two from by studying.

Thx for any direction.
christian.
Junior Member
 
Posts: 10
Joined: Thu Jun 09, 2011 10:44 am

Re: add edit button to dropdown field, maybe a new widget cl

Postby christian. » Thu Jun 09, 2011 5:22 pm

Okay, I tested my own proposal and it works but looks messy to me. I'd like to have the redirect() done in a more general way. Like a validator which is invoked for each widget every time the form is submitted. But instead of validating the widgets value, it would do stuff like redirection. It could be implemented in a separate class like sfWidgetFormPropelChoiceEditAction or sfWidgetFormPropelChoiceEditController to maintain the separation of view and controller. And one could implement a $form->processWidgets() method which tests every widget for such class and invokes the action under certain circumstances. Any ideas or comments?

Here is my quick and dirty approach:

I implemented a subclass in lib/widget/sfWidgetFormPropelChoiceEdit.class.php
Code: Select all
class sfWidgetFormPropelChoiceEdit extends sfWidgetFormPropelChoice
{
   public function render($name, $value = null, $attributes = array(), $errors = array())
   {
      $editButton = '<input type="submit" value="Edit" name="Edit' . $name . '" />';
      return parent::render($name, $value, $attributes, $errors) . $editButton;
   }
}


then exchanged the new dropbox with the existing one named address_id in lib/form/ContactForm.class.php:
Code: Select all
class ContactForm extends BaseContactForm
{
  public function configure()
  {
     $this->setWidget('address_id', new sfWidgetFormPropelChoiceEdit(array('model' => 'Address', 'add_empty' => '')));
  }
}


and changed the processForm() method in apps/frontend/modules/contact/actions/actions.class.php:
Code: Select all
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
   
    if ($form->isValid())
    {
      if(!is_null($request->getPostParameter('Editcontact[address_id]')))
      {
         $this->redirect('address/edit?id=' . $request->getPostParameter('contact[address_id]'));
      }

      $Contact = $form->save();
   [...]
christian.
Junior Member
 
Posts: 10
Joined: Thu Jun 09, 2011 10:44 am


Return to General discussion

Who is online

Users browsing this forum: No registered users and 5 guests