This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » legacy support » symfony 1.1 » Submit form to another action
Submit form to another action [message #90007] Tue, 15 December 2009 01:21 Go to next message
bazyl  is currently offline bazyl
Messages: 37
Registered: March 2008
Location: Poland
Member
Hello

I'm trying to upgrade my project from 1.0 to 1.1 and I have a problem with understanding some of the forms framework mechanisms.

I have an 'edit' action containing a form whch is submitted to another action called 'update'.

editSuccess.php
<form action="<?php echo url_for('user/update') ?>" method="post">

userActions.class.php
public function executeEdit() {
  $this->form = new UserForm($user); // $user retrieved from DB
}

public function executeUpdate() {
  $this->form = new UserForm(...);
  ...
  $this->form->bind($request->getParameter('user'));
  if($this->form->isValid()) {
    //save
  }
  // forward back to edit action
  $this->forward('user', 'edit');
}

And here's where the problems start. When the form is not valid, user is forwarded to the 'edit' action. I loose all the data stored in $this->form - the entered values and error messages.

I know I could wrap it all into one 'edit' action, but I don't want to mix the logic responsible for displaying the form with the one responsible for processing it.
This was way easier and intuitive in symfony 1.0. Is there any solution? Should I bind/validate the form again in edit action? Wink Or am I forced to always submit my form to the same action (the one which displays the form)?
It gets even more tricky if I have multiple forms on one page, and each form is submited to a different action. Should I process all of them in a single action?

Or maybe I'm just too used to the evil ways of sf1.0 which are no longer valid? Smile

Any help would be appreciated.
Re: Submit form to another action [message #90013 is a reply to message #90007 ] Tue, 15 December 2009 06:28 Go to previous messageGo to next message
cokker  is currently offline cokker
Messages: 582
Registered: January 2007
Location: Germany
Faithful Member
You don't need to execute the edit action. You can simply use the template by setting the template name.
Something like:

$this->setTemplate('Edit');


greets
Sven
Re: Submit form to another action [message #90019 is a reply to message #90007 ] Tue, 15 December 2009 10:32 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
If this writes to the database in a POST op, a redirect would be preferable in any case - otherwise the user may use forward/back and the browser will ask if the POST op should be repeated, which in the usual case is undesirable.


Remember Palestine
Re: Submit form to another action [message #90039 is a reply to message #90007 ] Tue, 15 December 2009 13:58 Go to previous messageGo to next message
bazyl  is currently offline bazyl
Messages: 37
Registered: March 2008
Location: Poland
Member
Thanks for the quick response.

@cokker - that's right and it's ok if I have just one form. But what if I have 2 separate forms on one page and each of the forms is submitted to a different action. It means that each of these action would have to set up the data for other forms.

@halfer - I agree with the forward/back buttons issue. But if i replace forward with redirect I'm still loosing the form data. So do you suggest the same solution as cokker?

Maybe I'm complicating things here and I should just use one action to handle all the forms within a page. Then simply use $this->setTemplate('Edit') in case of validation errors. I just thought I would be able to do it the way I used in symfony 1.0.

I'm trying to work out some common practices with the forms framework because I still have a lot of upgrading ahead of me. Thanks again for your help.
Re: Submit form to another action [message #90077 is a reply to message #90007 ] Wed, 16 December 2009 07:09 Go to previous message
cokker  is currently offline cokker
Messages: 582
Registered: January 2007
Location: Germany
Faithful Member
For pages with two or more forms I use a rather complex construction with components.

I have a main template "mainSuccess.php"
include_component('module_x', 'actionX', (isset($form_x)? array('form' => $form_x): array() );
include_component('module_y', 'actionY', (isset($form_y)? array('form' => $form_y): array() );


Each of this components is responsible to display one form. If parameter is not present a new Form is created, else the form from the parameter is used.

In the actions I do the usuall form handling, but in error cases I do the following:

$this->form_x = $this->form 
$this->setTemplate('main');


I hope this sounds not to weird and you get the idea.

greets
Sven
Previous Topic:'idservice' could not be found in the field names of type 'fieldName'.
Next Topic:sfRenderException
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software