Jump to content
louisstephens

Letting users "select" fields to add to a form

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.

Share this post


Link to post
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

Share this post


Link to post
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/

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Thanks @Gary Austin, this seems very similar to what I was going to do. For your "notes" pages, did you simply create the fields in the backend and assign them to the pages? How did you go about rendering/processing the fields? A simple:

$page->field_name

?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Here is the setup page for a note showing  one field under a category.  Each can be repeated.

beernote.thumb.JPG.94bfdddcc31b8336f290cc30a24a240d.JPG

Edited by Gary Austin
  • Like 1

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By gerritvanaaken
      My client could save some time if she could select multiple pages at once. Of yourse, we are using the standard page selector in multi-tree mode, but she has to select one page after another. Each time she has to open the site tree again and open the required subtree, finding the page she wants to select.
      Would be cool to let the tree open after selecting a page, so she could select multiple pages in one "tree session". Understand what I mean? Anybody any solution to this?
      Alternatively, maybe the site tree could keep its state with all the subtrees open, when you are re-open it again.
    • By Mithlesh
      Changing it to null
    • By louisstephens
      Going through my long quest to get better with ajax and utilizing the api, I have hit yet another roadblock. I currently have a form with an image field (thanks to flydev for getting that sorted), "title" text input, and a select field set to multiple. In my ajax call, I added in:
      tags = $("#select-tags").val(); form_data.append('tags', tags); $.ajax({ type: 'POST', data: form_data, contentType: false, processData: false, url: '/ajax/upload-preview/', success: function(data) { console.log("Woo"); }, error: function(xhr, ajaxOptions, thrownError) { alert(xhr.responseText); } }); And in the ajax template: 
      $tags = $sanitizer->text($_POST['tags']); $image = $sanitizer->text($_POST['image']); $p = new Page(); $p->template = "preview"; $p->parent = $pages->get("/previews/"); $p->name = $title; $p->title = $title; $p->tags = $tags; $p->save(); If I select a "tag" from the select input and submit, it does indeed add it to the Page Reference field in the backend. However, this does not work with an array being passed to it of multiple options.

      So it does appear that my ajax call is trying to submit multiple options, but I am really just unsure how to get these two added in. I saw in other forums posts of add($page) and even add(array()). Do I need to handle this js array differently or do  I need to foreach through the $tags to add it like:
      foreach($tags as $tag) { $p->tags->add($tag); $p->save(); } I tried this approach, but apparently I am still missing something.
       
      Edit:
      I was doing some tweaking, and I know I can split the js array out like:
      for (i = 0, len = tags.length; i < len; i++) { console.log(tags[i]); } However, I am not sure then how to handle the POST in php if I were to split it out.
    • By Tyssen
      I have a page that contains a single ProFields table field and I want to display the contents of the table on the front end and then for logged in users, they can edit certain columns in the table.
      What I have at the moment is
      $out = '<form action="'.$page->url.'" method="post" > <table class="table"> <tbody>'; $count = 1; foreach($page->fieldName as $row) : $out .= ' <tr> <td><input type="checkbox" name="fieldName_'.$count.'_columnName"></td> </tr>'; if($input->post->submit) : $page->of(false); $page->set('fieldName_'.$count.'_columnName', $sanitizer->text($input->post->{fieldName_'.$count.'_columnName})); $page->save(); endif; $count++; endforeach; $out .= ' </tbody> </table> <button class="button" type="submit">Save</button> </form>'; The two problems I have are:
      I get an error trying from $sanitizer->text($input->post->{fieldName_'.$count.'_columnName}), not sure how to make that dynamic.  If I change the above to just a static value, e.g. $page->set('fieldName_1_columnName', 'Testing') and save the form, it's not saving the values to the database. Where am I going wrong?
    • By VeiJari
      Hello forum. I'm trying to figure out how to detect if current form has errors after saving it in pw admin page. This is so that our event location isn't added in database if we detect errors. But I can't seem to find a correct way to detect errors? I only found the wire()->errors() and it always triggers even if I don't have any errors. 
      How can I detect errors in a form?
      By error I mean $page->error('this is the red error');
×
×
  • Create New...