Jump to content

Flexible input forms on a budget and with limited coding required


Kiwi Chris
 Share

Recommended Posts

Forms are an essential part of most websites, and it's no surprise that there's an excellent premium module Form Builder but what if you're on a zero budget for whatever reason?

It is possible to build forms quickly and easily by making use of a couple of free modules and the admin UI to give you a great deal of flexibility and speed of development, particularly if you need multiple forms on a website with different fields.

1. First you're going to need to install a couple of modules:

Form Template Processor

Fieldtype Select External Option

2. For each form that you want to display, create a template without a template file and add fields to it as you normally would. (eg I have formContact, formRegister etc)

Tip: under the Advanced tab in the setup for each template, I add a tag Forms so that all my forms templates are nicely grouped together in admin.

3. Create a new field of type Select External Option and call it formTemplate

In the section Create options from any database table select

  • templates as the source table
  • id as the Option Value
  • name as the Option Label

4. Create a new template file and call it renderForm.php (or whatever else you like)

  • Add an email field to this form - This will be the email address that forms get submitted to.
  • Add the formTemplate field you previously created to this form. This will allow you to select which of the templates you previously created such as formContact, formRegister etc you want to render.
  • Add any other fields as usual that you want to render on the page.
  • Add the following PHP code to the template file.
  • $recipient = $page->email;
    $form = $modules->get('FormTemplateProcessor');
    $form->template = $templates->get($page->formTemplate->label); // required
    $form->requiredFields = array('contactName', 'contactEmail', 'contactMesssage'); //Optional: This can be improved by having a field in the page template with a CSV list of required fields eg $form->requiredFields = explode(',', $page->requiredFields)
    $form->email = $recipient; // optional, sends form as email. FormTemplateProcessor can also save forms to the database.
    $content .= $form->render(); //generate the form to display.

    Note: this doesn't actually render the form at this point, but you have it in the $content variable ready to output wherever you want in your template.

  • Add any template HTML or other PHP code and echo $content; wherever you want to render the form.

5. Create a page using the renderForm template, and provide an email address, and select a form that you want to display.

6. Use CSS to style the form as required.

7. View your new page, and check that the form renders correctly.

8. You can modify the templates you created at step 2 or create new ones as required if your requirements for what fields forms display changes.

Note: The Form Template Processor module can also save form input as pages, and the FieldType Select External Option can be set up with filtering, so this solution can probably be refined further.

  • Like 9
Link to comment
Share on other sites

1 hour ago, szabesz said:

Thanks for sharing this tip! I will try it out as soon as I have the time but I already have one question: how do you fight nasty bots when this setup is used?

Good question. I'm not sure how good it is as it stands, but it is built on top of Processwire's form handling classes.

I think it would be pretty easy to modify Form Template Processor. There's already a property 'skipFields' to list fields from the template that are not rendered, and this is checked at both the point of rendering, and before sending an email.

It would be easy enough to add another property say 'hiddenFields' that are rendered but not output in email, and with a bit of CSS set up a hidden field that can be used as a honeypot.

  • Like 1
Link to comment
Share on other sites

Had to play around with it and it works like a charm.

Thank you @Kiwi Chris for sharing this. This will come in handy for smaller sites or prototypes.

What I really like is that saving pages from the submitted form doesn't need an extra step as it does in FormBuilder.
The created template is already the form and the template for the pages.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...