<p>I wouldn't worry going so far outside of Craft and using Yii's forms.</p>

<p>Sounds like all you need is a Craft <a href="http://buildwithcraft.com/docs/plugins/introduction" rel="nofollow">plugin</a>.  Use normal HTML <code>&lt;form&gt;</code>s from  your template and have them post to your plugin's <a href="http://buildwithcraft.com/docs/plugins/controllers" rel="nofollow">Controller</a>.  Each step in the form can be another <a href="http://buildwithcraft.com/docs/plugins/controllers#posting-to-controller-actions" rel="nofollow">action</a> on the controller.  You can persist data between form steps by saving to Craft's <a href="http://buildwithcraft.com/classreference/services/HttpSessionService" rel="nofollow">HttpSessionService</a>, which is just a thin wrapper for PHP's global <code>$_SESSION</code> variable.</p>

<pre><code>craft()-&gt;httpSession-&gt;add($key, $value)
</code></pre>

<p>and:</p>

<pre><code>craft()-&gt;httpSession-&gt;get($key)
</code></pre>

CC BY-SA 3.0
2015-08-26T07:32:06.840Z
Thanks for the tips Brad. I thought using Yii's forms would give me the benefit of being able to use its validation, which seemed like a good idea.
Phil Gyford
CC BY-SA 3.0
2015-08-26T15:42:06.263Z
You can still take advantage of validation.  Just give your plugin a [Model](http://buildwithcraft.com/docs/plugins/models) and your controller can call `validate()` on it.
Brad Bell
CC BY-SA 3.0
2015-08-26T15:54:31.640Z
Ah, thanks Brad. I'll have a think. This is a large multi-step application form, and it doesn't really match up with the traditional sense of 'models' very well, but maybe I can make this work.
Phil Gyford
CC BY-SA 3.0
2015-08-25T17:49:34.073Z
2015-08-25T17:49:34.073Z
Brad Bell
<p>I have a Craft-based website and need to make a complex multi-'page' form on one page. The form isn't related to any Craft Entries etc, and doesn't need to save the submitted data to Craft's database (it's sent to a different database).</p>

<p>Having looked at a few form-related Craft plugins, I think I'll be best writing this from scratch. I'm experienced with PHP, but not Yii and I'm not quite sure how best to structure this within a page that will be generated by Craft (an Entry).</p>

<p>I'm guessing I could write a plugin and a variable in it is used in the Craft template to generate the form. Given that, some questions:</p>

<ol>
<li>Should I create a series of <a href="http://www.yiiframework.com/doc-2.0/guide-start-forms.html" rel="nofollow">Yii forms</a>, one per 'page' of the form, and use the one indicated by a hidden 'step' form field?</li>
<li>Is there any issue with saving data to Yii sessions (for each form step's inputs) within a Craft site?</li>
<li>I could have templates for the different form 'pages' in my plugin. How would I pass data from the form (fields, submitted data, error messages) to the template?</li>
</ol>

<p>I guess I'm also looking for general responses of either "This looks like a sensible route to explore" or "OMG don't go that way, there are monsters!" :)</p>

<p>EDIT: I think I should be using the plugin's <a href="http://buildwithcraft.com/docs/plugins/controllers" rel="nofollow">Controllers</a> to handle form submission, passing data to templates etc. Rather than having a plugin variable in a Craft Entry's template. Maybe I can integrate Yii forms with those Controllers?</p>

<form><yii>
Multi-page form with Yii in a Craft site?
# Multi-page form with Yii in a Craft site?

(Asked by: username_0 on 2015-08-25)

I have a Craft-based website and need to make a complex multi-'page' form on one page. The form isn't related to any Craft Entries etc, and doesn't need to save the submitted data to Craft's database (it's sent to a different database).

Having looked at a few form-related Craft plugins, I think I'll be best writing this from scratch. I'm experienced with PHP, but not Yii and I'm not quite sure how best to structure this within a page that will be generated by Craft (an Entry).

I'm guessing I could write a plugin and a variable in it is used in the Craft template to generate the form. Given that, some questions:

1. Should I create a series of [Yii forms](http://www.yiiframework.com/doc-2.0/guide-start-forms.html), one per 'page' of the form, and use the one indicated by a hidden 'step' form field?
2. Is there any issue with saving data to Yii sessions (for each form step's inputs) within a Craft site?
3. I could have templates for the different form 'pages' in my plugin. How would I pass data from the form (fields, submitted data, error messages) to the template?

I guess I'm also looking for general responses of either "This looks like a sensible route to explore" or "OMG don't go that way, there are monsters!" :)

EDIT: I think I should be using the plugin's [Controllers](http://buildwithcraft.com/docs/plugins/controllers) to handle form submission, passing data to templates etc. Rather than having a plugin variable in a Craft Entry's template. Maybe I can integrate Yii forms with those Controllers?

**Tags:** form, yii

---

## 1 answers

### Answer by username_1 on 2015-08-25. Score: 2

I wouldn't worry going so far outside of Craft and using Yii's forms.
Sounds like all you need is a Craft plugin.  Use normal HTML s from  your template and have them post to your plugin's Controller.  Each step in the form can be another action on the controller.  You can persist data between form steps by saving to Craft's HttpSessionService, which is just a thin wrapper for PHP's global $_SESSION variable.
craft()->httpSession->add($key, $value)

and:
craft()->httpSession->get($key)

> **Comments:**
> **username_0**: Thanks for the tips Brad. I thought using Yii's forms would give me the benefit of being able to use its validation, which seemed like a good idea.  
> **username_1**: You can still take advantage of validation.  Just give your plugin a [Model](http://buildwithcraft.com/docs/plugins/models) and your controller can call `validate()` on it.  
> **username_0**: Ah, thanks Brad. I'll have a think. This is a large multi-step application form, and it doesn't really match up with the traditional sense of 'models' very well, but maybe I can make this work.  

---

