Forum

Thread tagged as: Question, Problem, Forms

Create Form based on Collection content

Hi,

I'm trying to create a form that will create a set of form items based on the contents of a Collection. The aim is to allow users to register interest in items from a Collection of products.

I'm calling the collection to get the form output I need and then passing this into my form. Here's the collection call:

opts_bikes = perch_collection('Rentals Facts', [
        'template'   => 'collections/_rentals-form-options.html',
], true);

And then I set it as a variable to pass into the form template:

PerchSystem::set_var('opts_bikes', $opts_bikes);

And finally I have my form template:

<perch:form id="bookings-form" method="post" class="" app="perch_forms">
    <perch:forms id="opts_bikes" encode="false" />
    <perch:input type="submit" value="Search" />
</perch:form>

The output on the front-end is as I need it and devTools shows the form inputs with the is and labels as expected. The form will also submit and register in Perch. But I don't get any form content through — the user's form input isn't displayed in the Forms admin.

Is what I'm trying to do possible? Should I be able to see the user's form input? Am I taking the right approach here?

Many thanks in advance for any help.

Simon Kelly

Simon Kelly 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

The fields need to be in the form template else the values won't be picked up.

Thanks for the quick response, Drew.

Is there anything you could suggest to achieve what I'm trying to do? Or is this just not possible with the Forms app?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, template the items in the form template, not in a different template first.

The field will need to exist in the form template. You can dynamically set its options because content tags are parsed before input tags.

Thanks, Drew.

I'm a bit confused, I'm afraid. My form fields need to reflect what's in the products Collection. So I need to make a call to the Collection to find out first which products to show on the form. How can I do this in the form template? I didn't think that I can run a php collection call within a form template.

Perhaps I've misunderstood what you're saying? Could you guide me a little more?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Simon,

I don't know what fields are added by your template _rentals-form-options.html so it's hard to be specific.

But if you have a select field for example:

<perch:form id="bookings-form" method="post" class="" app="perch_forms">
<perch:input type="select" id="colours" options="red,green,blue">
<perch:input type="submit" value="Search" />
</perch:form>

The value of options can be set dynamically:

<perch:input type="select" id="colours" options="<perch:forms id="colour_opts">">

Thanks for the reply and suggestion, Hussein.

You're right, I think I need to be more specific about the form options!

In my _rentals-form-options.html template I had the following (which uses fields from the Collection template):

<div class="c-form-item">
    <perch:label for="<perch:content id="slug" for="title" type="slug" />"><perch:content id="title" type="text" /></perch:label>
    <perch:input type="text" id="<perch:content id="slug" for="title" type="slug" />" label="<perch:content id="title" type="text" />" />
</div>

The aim is to output a label and text input for each item in my products Collection. I still don't see how I could achieve that dynamically though. Any ideas would be very appreciated!

Hi,

I've returned to the project involving this issue and would like to clarify my question to see if what I'm looking for is possible.

I'm trying to create a set of checkboxes based on the content of a Collection. If the Collection contains 7 items, my form would display a checkbox for each of the 7 items.

I've seen a couple of older forum posts that indicate that this might not be possible (https://forum.grabaperch.com/forum/01-18-2016-create-dynamic-number-of-checkboxes-using-form-app-based-on-content?page=1 and https://forum.grabaperch.com/forum/12-23-2014-pass-an-array-into-form-template-can-i-do-this).

I can get the mark-up into the form correctly using the method I mentioned initially above, but that doesn't work (because as Drew said, the fields need to be in the form template in order to be picked up). Is it at all possible to include the set of checkboxes dynamically (rather than hard coding them, for example)?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is it at all possible to include the set of checkboxes dynamically (rather than hard coding them, for example)?

No, you can't do it.

Thanks for confirming that, Drew.

I've tried to get around this by using a select with a multiple attribute but it looks like this doesn't work (as referenced also in this forum post https://forum.grabaperch.com/forum/02-04-2016-select-element-with-multiple-attribute-arent-saved). Is is still the case that a multiple select isn't supported in Forms app?

Is there anything you can suggest to achieve the checkboxes display that I'm looking for? I understand that this isn't possible with the Forms app but if you could give me any pointers on how I could approach this otherwise, that would be really useful.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can't do it. That functionality just doesn't exist.

OK, thanks for confirming Drew.