Jump to content

Displaying the children in the Page Inputfield?


Nelson
 Share

Recommended Posts

Hi there,

Here I come again with, yet another tricky question, sorry. :)

I would like to know if it's possible (using a Select output in the Page Inputfield) to display the Pages as optgroup tags and the children as options tags?

I'll explain my goal. At our company we offer a range of courses (the pages) and each course has many editions (the children) along the year.

At the moment I'm converting to Processwire (with the FormTemplateProcessor Module) a registration form that acts as main gateway for people who want to participate in our courses.

The way people select which course/edition they to participate in, is by using a drop down list like this:

<select name="Courses" id="Courses">
<optgroup label="Course">
<option value="Course - Edition A">Edition A</option>
<option value="Course - Edition B">Edition B</option>
</optgroup>
</select>

I'm asking this because I'd like to use the FormTemplateProcessor Module for validation and sanitization purposes.

Thank you very much,

Nelson

Link to comment
Share on other sites

The InputfieldSelect does support optgroups (for your self generated selects), but InputfieldPage doesn't use them. So to do what you are wanting there, you'd probably need to make something custom. To make option groups with InputfieldSelect, do them like this:

$select = $modules->get('InputfieldSelect');
$select->attr('name', 'name_of_select');  
$select->addOption('My option group', array('Option A', 'Option B', 'Option C')); 
$select->addOption('Another option group', array('Option 1', 'Option 2', 'Option 3')); 
$select->attr('value', 'Option B'); // example

// render here, or add to an InputfieldWrapper/InputfieldForm/InputfieldFieldset
echo $select->render();

Another route that you could use now would be to just title your selectable pages in a manner that reflects the group, i.e. "Course: Edition A" rather than just "Edition A".

  • Like 1
Link to comment
Share on other sites

Thank you for the reply Ryan.

Do you think If I fully create the form (template) dynamically with the API instead of using the prebuilt form I have now, will I be able to use the validation provided by the FormTemplateProcessor Module?

Only if there was a way to somehow push this field into the template provided to the FormTemplateProcessor module...

Link to comment
Share on other sites

Yes, if you create a form with Inputfields manually then calling the processInput() method on the InputfieldForm will process and validate the fields. In your case you could do something like the previous example with InputfieldSelect, but you could always populate it with options of $page->id => $page->title or something like that. When you want to specify separate values and labels for options, you just make the array associative. I'll repeat a part of the example above, except modify it to specify separate value and label:

$select->addOption('My option group', array(
 'A' => 'Option A', 
 'B' => 'Option B', 
 'C' => 'Option C'
 )); 
Link to comment
Share on other sites

Hi Ryan!

Thank you for your reply,

Sorry for not replying till now, I've been a bit busy trying to work this out and it seems I'm finally getting the hang of this. :)

Basically I'm building a module based in your FormTemplateProcessor but without using the InputfieldPage to store the fields. Instead I'm storing them in array of Inputfield instances. This gives me the ability to use any type of field which solves my first issue, it also validates which is sweet! :)

But I noticed something, after a Form is processed the fields seem to lose their original type somehow. I think this is because I'm not storing the fields inside a Page instance.

This was only noticeable inside the sendMail method when checking for a InputfieldEmail instance. It's not really an issue, just something I noticed.

Thank you very much for your help! :)

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...