Jump to content

Letting users "select" fields to add to a form


louisstephens
 Share

Recommended Posts

So I have been creating an internal web app over the last couple months, and am finally on the last piece of the puzzle, customizable forms. I thought I could just create a view basic forms and use a select option to let them select the form, and my template file could just output the form based on their selection. Easy enough. However, I have since ran into a slight head scratcher. Not all the forms are the same and they might vary greatly between each other. 

Has anyone done something similar to this? In an ideal world, I would use form builder, but I really dont want them to have access to it. They are currently constrained to a few pages in the backend, which is why I was trying to think of a way for them to select a few fields (like from a page select), and then render the selections on the front end based on what was selected.

Link to comment
Share on other sites

45 minutes ago, louisstephens said:

However, I have since ran into a slight head scratcher. Not all the forms are the same and they might vary greatly between each other. 

I'm not sure I understand this part. Can you elaborate? Do you fear that over time you'd end up with too many forms, and everything becomes chaotic? (endless variations)

  • Like 1
Link to comment
Share on other sites

Thanks for the reply dragan.

Currently, my strategy was to use a select options box, ie:

1=basic-form|Basic Form
2=enhance-form|Form Form
etc etc

And in my template, use a foreach:

<?php

if($page->form_select->title === "Basic Form") {
	echo "{form that I have predefined";
} else if($page->form_select->title === "Enhanced Form") {
	echo "{form that I have predefined";
}

else if(...) {}
else {}

?>

The current set up works without issue and is pretty straight forward. However, it means I have to spend my time creating new forms due to a user wanting their own custom form and not sticking to a default . Understandably, my basic forms might not be a catch all for their needs. Now, the forms wont get too crazy, but some might need first + last names combined, whereas some need them split (for various reporting needs). I guess I am trying to devise a way to give everyone what they want with a blanket solution, without me having to create 40+ form variants and cause my if statements to be crazy.

Aside from the structuring, each form might have to be sent to different email address with different subjects, but I have that under control with a few fields on their pages which directs wiremailsmpt correctly.

Just thinking aloud, but maybe the closest to what I want is by using a page selector, and have the "pages" just be placeholders for the form elements, Ie

First Name

Email

Address

 

And in my template -->

<?php
	


	$field = $modules->get("InputfieldText");
    $field->attr('id+name','firstname');
    $field->attr('type','text');
    $field->attr('placeholder', 'First Name');
    $out = $field->render();
    echo $out;
?>

However, this too will probably end up close enough to my first example/

Link to comment
Share on other sites

12 minutes ago, louisstephens said:

and cause my if statements to be crazy.

That's why you should use switch() instead :-)

Do you only have text and textarea fields? How would you deal with radio buttons and checkboxes? This would be no easy task, because the editors would want to define those labels and values. Also, one author would want to make one field mandatory, while another won't. These are things you couldn't accomplish with the page selector route. Furthermore, thinking about layout - I'd expect that you could somehow define width of each field ( with percentages or a grid-like system).

What's the reason you don't want to enable your client to use FormBuilder?

  • Like 1
Link to comment
Share on other sites

I guess my "issue" was having them in the setup tab and leaving their current page. I have tried to make the actions they can perform very straight forward/easy, as some of them are not very "tech savvy". I personally like what Form Builder (have not used it yet, but will be soon) offers, and it will be ideal for when another dev or myself use it.

Unfortunately, having a long list of contact forms on a page for them all to see might create some confusion and result in editing the "wrong" form not applicable to them.

Link to comment
Share on other sites

So I have a bunch  of templates that define fields for different types of "notes".  I put them under a parent page and then use a Page Reference field on the parent page of the templates.  The user can select from the templates using a radio box (I allow multiple options, but you could use other input options to just get one).  I later then process their templates as needed by reading in the template page and processing it.  To add a new template I just add a page and it is then available without having to edit the option box.

I may also use a loop on the children of the template parent with my own code to generate the select options.  This would allow the templates having  an extra value to indicate which group of user can see them.  In this case a page reference field would not work easily (that I know of) as I want to filter the templates based on the "type" of user or some other property the user has.

  • Like 1
Link to comment
Share on other sites

 

I started with a big multi-level repeater field to allow adding an input, specifying its name and picking type (text,select,multiselect) and then adding options if allowed.  I then saved this as a new template by writing json to a file with a name keyed to type of note.  When the user selects to start a note I pull the corresponding json file and generated the form.   I had planned to offer some option with repeater fields to users down the road for generating their own, but right now it is too difficult as notes have 30 or more input select fields with lots of options for each note.  For now I allow a few customization options for each user that gets stored also in a json/text field and then "merge" with an existing note as I generate the final form.

You could certainly just write the note html versus intermediate structure, but I needed an easy way to allow the user to change select options and the json structure allows me to match the original structure with the user customization at final generation.

Link to comment
Share on other sites

Some setup like this might do the trick:

Create a table or repeater / matrix repeater field.

Backend users could then create a form from scratch having these fields:

  • type of field (dropdown / radio button)
  • name of field
  • field label (perhaps use PWs sanitizer to create field-name from this)
  • optional text shown beneath the label or wherever (nice to have)
  • checkbox "required?"
  • a textarea field that users would have to fill in for radio buttons, checkboxes or selects (each line equals an option)
  • Maybe additional fields for setting notification messages, cc: or similar stuff.

The only caveat I could foresee: multilanguage setup.

Link to comment
Share on other sites

As far as rendering, for PW side I just generate form html using inputfield rendering as I loop thru the json (converted to PHP array) and generate the form.   I use this technique to generate a version of the form with the ability for the user to modify the option selects and add an extra field or two.   As I said I merge those in with the "fixed" data for each type of note.  

I also use the same json form data to generate the form and organize the display of note data in javascript on the client side as this is actually for a web app.

Link to comment
Share on other sites

Thanks everyone for the great advice! I am testing the waters right now using the module @adrian mentioned, and thus far everything is going great. My only real hurdle is getting the form submit portion. I am using wireMailSMPT , and I cant figure out really how to change out the $text = ""

<?php
	$firstName = $sanitizer->text($input->post->form_first_name);
	$lastName = $sanitizer->text($input->post->form_last_name); //needs to be switched out if fullname is selected
	$email = $sanitizer->email($input->post->email);
	$phone = $sanitizer->text($input->post->phone);


	$textBody = "
    First Name: $firstName 
    Last Name: $lastName //needs to be switched out if fullname is selected and not partials
    ";
	$numSent = wireMail($yourEmailaddress, '', $subject, $textBody, $options);

?>

 

variable based on what is selected from the form {made even more complicated as the field select is inside a few matrix repeaters). I guess perhaps I have made it way to difficult as the field inputs are now dynamic, and now my form processing needs to be as well.

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