2

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, 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 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?

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 <form>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)
| improve this answer | |
  • 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 Aug 26 '15 at 7:32
  • You can still take advantage of validation. Just give your plugin a Model and your controller can call validate() on it. – Brad Bell Aug 26 '15 at 15:42
  • 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 Aug 26 '15 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.