XThreads 1.6* has many new features. Some of them are
Input Formatter and
Custom Modify Error. Maybe we can use it for creating thread fields dependencies.
The basic idea is, display a custom fields if a user selects a specific value from a lists.
There are too many possibilities for this (very depends on what we want to do with our form), so just modify it as our needs.
In this very simple example, we will:
- Display textbox 1 if a user selects List #1 value from a listbox, and force the user to write something in the textbox. Whatever user entered here, the textbox value will be saved only if the user select List #1 from the listbox. Only display this thread fields rows on showthread if there is value supplied for this fields.
- Display textbox 2 if a user select List #2 value from the listbox, and force the user to write something in the textbox. Whatever user entered here, the textbox value will be saved only if the user select List #2 from the listbox. Only display this thread fields rows on showthread if there is value supplied for this fields.
- Do not display those textboxes if the user selects List #3 value from the listbox, and do not force the user to write something on those textboxes. No value will be saved from the textbox 1 and the textbox 2. Do not display those fields rows on showthread page.
First, we need to create our list:
Then create a textbox:
This textbox will be displayed if the user select List #1 from our Listbox above.
- Title: Textbox List #1 Selected
- Key: tb1
- Input Field Type: Textbox
- Display Order: 2
- Editable by / Required Field?: Everyone
We need to make this field as a non required field. We'll make it as required field only if the user select List #1 from the listbox (look at the Custom Modify Error setting).
- Input Formatter:
We only save the value entered in this textbox if the user select List #1 from our listbox above. If the user selects other values, whatever user enters here won't be saved.
- Custom Modify Error:
It makes this field is required if the user selects List #1 from the listbox.
Basically, we can use the Custom Modify Error feature for custom fields validation.
- Blank Replacement Value:
Because we don't hide this field from the showthread, so we need to hide it if there is no value supplied for this field (look at the showthread_threadfield_row template).
There are some ways to do this. We can use Template Conditional to manipulate it directly in our template.
Then create other textbox:
This textbox will be displayed if the user select List #2 from our Listbox above. The settings are similar to the textbox above.
- Title: Textbox List #2 Selected
- Key: tb2
- Input Field Type: Textbox
- Display Order: 3
- Editable by / Required Field?: Everyone
- Input Formatter:
- Custom Modify Error:
- Blank Replacement Value:
Then create new templates in our Global Templates (use template prefix if needed):
- newthread and editpost_first
We need to put our javascript code to hide/show the textbox 1 and textbox 2 based on user selection from the listbox in these templates. Based on the example above, we can put this js code after the threadfields defined in these template:
I'm totally zero with js. Maybe you need to write your own function for your purpose.
- post_threadfields_inputrow
Basically, we need to put an id to the thread fields rows in newthread/editpost pages.
- showthread_threadfield_row
Basically, we need to put an id to the thread fields rows in showthread page (look at the Blank Replacement Value setting for the textbox 1 and the textbox 2).
That is our very simple example for creating XThreads custom thread fields dependencies.
If there is something wrong with the code and how this is implemented, please let me know. I'm still learning.