How to Save a Form via Code then redirect to a route
Created 6 years ago by edster

I 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');
    }
ryanthompson  —  6 years ago Best Answer

@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!
edster  —  6 years ago

Also tried $form->setEntry($id); instead of build

william  —  6 years ago

Tried : this->redirect->to() ?

edster  —  6 years ago

That has the same effect :/

edster  —  6 years ago

Ok, nevermind your redirect works, however I don't know how to do this saving thing.

The getModel() method is just returning null Call to a member function setActivation() on null

ryanthompson  —  6 years ago Best Answer

@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!
edster  —  6 years ago

@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.

ryanthompson  —  6 years ago

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  —  6 years ago

Thanks!