conditionally applicable required form fields?

1,072 views
Skip to first unread message

Craig

unread,
Apr 27, 2012, 3:17:40 PM4/27/12
to joomla-de...@googlegroups.com
I need to create a form for adding a vehicle that has different fields depending on the fuel type. The additional fields are stored in tables with a foreign key to the vehicle table. 

What I'd really like is for the additional fields to pop in dynamically when the fuel type is selected, so the user remains on a single form page. The issue I'm having is that I have a single form xml file and some of the additional fields will be required if they are applicable, and I want to continue to use Joomla server side validation. 

My original thought was to hide and show (and disable/enable) the additional fields with javascript as appropriate and override the validation to only have applicable fields validated. Is this feasible? Could you point me in the right direction for this?

My next thought was to use layouts - have the user select the fuel type first and then redirect to the form page with layout overrides per fuel type. My approach here would be to try to have an xml form file per layout with that type's applicable fields. Is this feasible? Does it make sense to include fields from multiple xml form files into a single html form? If so, how would I do this?

My latest thought is that being a Joomla newbie might make this harder than it should be - is there already a Joomla idiom for this?

Mark Dexter

unread,
Apr 27, 2012, 4:07:04 PM4/27/12
to joomla-de...@googlegroups.com
Either using JavaScript or using layouts is feasible. Some of it depends on how many combinations and fields there are. For example, we have a JavaScript behavior called behavior.switcher that is used in the Global Configuration screen. If you look at that user interface, each tab page is accomplished by disabling all of the fields on the form except for the one tab -- more or less like you were thinking about. You can see the code in administrator/components/com_config/views/application folder.

If you don't mind having a Submit button after the fuel type is entered, you could use a plugin to alter the form using the onBeforeContentPrepareForm event to disable the fields you don't need (or add just the fields you do need).

Either of those approaches would be fairly straightforward to implement. The first one would probably be the easiest, as long as you don't have too many different combinations.

Good luck. Mark


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/wnqO3N5JxRwJ.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

Craig

unread,
Apr 27, 2012, 4:54:44 PM4/27/12
to joomla-de...@googlegroups.com
Thanks for the thoughtful response.

I don't think the Global Configuration tabs handle the server side validation the way I want to - here is the scenario:
1. On the Server tab, I deleted the SMTP port value (which is required)
2. I switched to another tab and hit Save and Close - and nothing happened (b/c the empty SMTP port value on the unselected Server tab is invalid)

I think a combination of your 2 suggestions could work for me - show just the applicable fields with Javascript and use onBeforeContentPrepareForm to remove the extraneous fields.

Thanks for your help,
Craig


elin

unread,
Apr 27, 2012, 6:59:12 PM4/27/12
to joomla-de...@googlegroups.com
Another good place to look is in the menu item where additional fields are added based on the value selected in the menu type modal.

You can always reference the saved value of  a field in a form to control the display of other parts of the form using php in the layout (you don't need to use a plugin if it is your  own form rather than modifying an existing form).
To make a field not validate you can unset it conditionally. 

I don't know if you have looked at the equals validation, but that validates one field using a value from another field (checking that the two password fields are equal e.g). So you could write a custom rule that makes validation conditional on values in other fields. It might be a pretty complex rule but it sounds like you do have complexity.

The other thing to take advantage of is the group parameter which we don't really use in the core but is quite useful, so you can just load group $type and load different fields based on other variables (this is how jcontent works but it's just using jform).

Elin

subtextproductions

unread,
Apr 30, 2012, 12:58:26 PM4/30/12
to joomla-de...@googlegroups.com
The JForm object has some great methods for dealing with exactly this situation. You can do validation on the front end using javascript, but you can also change the "required" and "validation" properties on the individual field objects of the JForm object. Here's a quick and dirty sample of code in the model. In this example, $this refers to the JModel class or one of its child classes.

// GET THE DATA FROM THE POSTED FORM
$data = JRequest::getVar('jform', array(), 'post', 'array');
// USE THE MODEL CLASS TO LOAD THE FORM OBJECT.
$form = $this->getForm($data);
// GET THE VALUE OF A SPECIFIC FIELD
$a = $form->getValue('a');
// IF THE VALUE OF a IS ZERO, THEN b IS NOT REQUIRED
if((int)$a == 0){
  $form->setFieldAttribute('b', 'required', 'false');
}
// VALIDATE THE FORM BASED ON CHANGES YOU MADE
$form->validate($data);

That's just a sample of the code that you could use, but the basic structure should hold true for any situation you can dream up. By default fields are either required or not, and they may or may not have filters set. By using the setFieldAttribute() method, you can change the default behavior of the form as defined in the forms XML file. You can totally rewrite the structure of the form on the fly by changing field attributes and adding or removing fields from the form object.
Reply all
Reply to author
Forward
0 new messages