[FLOW3-general] Edit Form with two seperate target actions

Jan Greth jan at greth.me
Fri Sep 28 11:30:17 CEST 2012


Hey John,

now I found a working solution. Bit ugly, but it works... :)

Buttons in my form:
/*********************************************************************/
<f:form.submit value="Speichern und zurück" name="saveAndBack" />
<f:form.submit value="Speichern" name="saveAndBack" />
/*********************************************************************/


And the Controller:
/*********************************************************************/
	/**
	 * Updates the given location object
	 *
	 * @param \JPG\Trainings\Domain\Model\Location $location The location 
to update
	 * @return void
	 */
	public function updateAction(\JPG\Trainings\Domain\Model\Location 
$location) {

		if(($location->getLan() == '')||($location->getLat() == '')) {
			$this->findCoordinates($location);
		}

		//Get request arguments
		$args = $this->request->getArguments();

		if($args['saveAndBack'] == 'Speichern'){
			$this->locationRepository->update($location);
			$this->addFlashMessage('Updated the location. Redirect to Edit.');
			$this->redirect('edit', 'Location', NULL, array('location' => 
$location));
		} elseif($args['saveAndBack'] == 'Speichern und zurück') {
			$this->locationRepository->update($location);
			$this->addFlashMessage('Updated the location.');
			$this->redirect('index');
		}

	}
/*********************************************************************/

Ugly? Yes, because I'm asking for the value (text displayed) of the 
button. That might be a problem when different languages oder text 
changes are done. But I think, when using the translation handling, I 
can minimize the risk - or?

Bye,
Jan



Am 28.09.2012 10:31, schrieb Jan Greth:
> Hi John,
>
> thanks for your advice. But when I use the Buttons
>
> /*********************************************************************/
> <f:form.submit value="Speichern und zurück" name="saveAndBack" />
> <f:form.submit value="Speichern" name="saveAndBack" />
> /*********************************************************************/
>
> and then try to make a decision in my controller / action
>
> /*********************************************************************/
> /**
>   * Updates the given location object
>   *
>   * @param \JPG\Trainings\Domain\Model\Location $location The location
> to update
>   * @return void
>   */
> public function updateAction(\JPG\Trainings\Domain\Model\Location
> $location, $saveAndBack = 'default') {
>
> if(($location->getLan() == '')||($location->getLat() == '')) {
>      $this->findCoordinates($location);
> }
>
>
> if($saveAndBack == 'Speichern und zurück'){
>      $this->redirect('edit', 'Location', NULL, array('location' =>
> $location));
>      } elseif($saveAndBack == 'Speichern') {
>          $this->locationRepository->update($location);
>          $this->addFlashMessage('Updated the location.');
>          $this->redirect('index');
>      }
> }
> /*********************************************************************/
>
> I get "Uncaught Exception in FLOW3"...
> See: http://pastebin.com/jSeZbr5G for the full Exception.
>
> What am I doing wrong?
>
> Bye,
> Jan
>
>
>
>
> Am 27.09.2012 20:28, schrieb John Small:
>> Hi,
>>
>> not tested in this specific way,
>>
>> you could name your submit-buttons then you have the value of the
>> clicked button in your action-method.
>> then in the action-method you can choose where to go with different
>> redirect-calls depending on that value.
>>
>> To use the additional value in your action method just add it in the
>> header of the method:
>>
>> public function updateAction(Someobject $someobject, $buttonname =
>> 'default') {
>>      ...
>>
>>      if ($buttonname ...)
>>          ...redirect('here');
>>      else
>>          ...redirect('there');
>> }
>>
>>
>> if you have to provide an object for the action method you want to
>> redirect to add it to the redirect call, e.g.
>>
>>      $this->redirect('show', 'Controllername', NULL, array('someobject'
>> => $someobject));
>>
>>
>> HTH
>>
>>
>> Am 27.09.2012 15:11, schrieb Jan Greth:
>>> Hey Folks,
>>>
>>> I have an simple Edit-form like the one autogenerated by the FLOW3 CLI.
>>> Looks like this:
>>>
>>> /*********************************************************************/
>>> <f:form action="update" object="{location}" name="location">
>>>   <ol>
>>>     <li>
>>>       <label for="title">Title</label>
>>>       <f:form.textfield property="title" id="title" />
>>>     </li>
>>>
>>>     <li>
>>>       <label for="street">Street</label>
>>>       <f:form.textfield property="street" id="street" />
>>>     </li>
>>>
>>> [...]
>>>
>>>     <li>
>>>       <f:form.submit value="Save and back"/>
>>>     </li>
>>>   </ol>
>>> </f:form>
>>> /*********************************************************************/
>>>
>>> But now I have the problem that I would like to have two send buttons.
>>> One for "Save and back" and one just for "Save" (User stays on page)
>>>
>>> How would you implement it?
>>>
>>> I had thoughts of writing a new action - but Action is set in form
>>> head not in button...
>>>
>>> Bye,
>>> Jan
>>
>



More information about the FLOW3-general mailing list