How do I redirect a FormBuilder that's included in a view?

daviesgeek - 1 week ago

I have a FormBuilder like so: $builder. I am including the form on a page with a controller like this:

return $this->view->make('module::view', ['form' => $builder->make()->getFormContent()]);

And in the view:

<h1>My Cool Form!</h1>
{{ form | raw }}

I've set the redirect option on the form:

$builder->setOption('redirect', 'my-other-page');

But I can't get the form to redirect after submission. If I return the rendered builder:

return $builder->render();

The redirect works, but not if I include it in a page like stated above. How can I do this?

ryanthompson - 1 week ago

You can access options and methods using the form function which returns a criteria instance wrapping the builder you chose. This is not documented yet but here's what to do:

  • First bind the builder class to a string like my_form
  • Then use the form method to open that form: {{ form('my_form') }}

You can also magically resolve your form builder if your model is bound to generated one and calling form like this: {{ form('namespace', 'stream') }}.

After that you can access methods and options in a fluent manner:

{{ form('my_form').redirect('thanks').successMessage('You rock ' ~ user().username)|raw }}

You can also set the form and use it's components in the FormPresenter:

{% set form = form('my_form').redirect('thanks').successMessage('You rock ' ~ user().username).get() %}

{{ form|raw }}

{{ form.open|raw }}
{{ form.fields.name|raw }} \\ Display the "name" field.
{{ form.fields.name.input|raw }} \\ Display the "name" input only. Name returns the field type / presenter 
{{ form.close|raw }}