How to Save a Form via Code then redirect to a route
Created 7 years ago by edsterI am trying to update an existing entry, then redirect to a route upon saving. But can't seem to get it to work. This is what I currently have, this is just loading up an edit page instead of saving then redirecting.
/**
* Activate an existing entry.
*
* @param AccessFormBuilder $form
* @param $id
* @return \Symfony\Component\HttpFoundation\Response
*/
public function activate(AccessFormBuilder $form, $id)
{
$form->build($id);
$form->getModel()->setActivation(TRUE);
$form->saveForm();
return redirect()->action('AccessController@index');
}
@edster Check out how core does it: https://github.com/anomalylabs/streams-platform/blob/1.3/src/Ui/Form/FormHandler.php#L24
$builder->saveForm(); // Fires all the model saving magic
And you would want to set the response manually on the form like this:
$builder->setFormResponse(redirect('foo')); // Now the form won't try and come up with it's own response. It'll use yours!
@ryanthompson Didn't see the setFormResponse()
before, thanks. I tried to use saveForm()
but it wasn't working.
I wonder if i wasn't setting the value properly? What is the correct way to do that?
I ultimately went with, I'm making this harder then I have to, and just loaded up the model and updated the entry that way.
Ya don't force yourself into a pattern if it doesn't make sense lol. Form values though.. would need to be like this:
$builder->setFormValue($key, $value);
@edster Check out how core does it: https://github.com/anomalylabs/streams-platform/blob/1.3/src/Ui/Form/FormHandler.php#L24
And you would want to set the response manually on the form like this: