0

Is there a simple way to have several fields within a field group; such a way that only one field in the group can be populated.

Example use Case Field group is called 'Insert Media'.

The group contains - an image field - an audio field - a video field

The user may complete one field, but can not complete multiple fields within the group.

1

I have not expressly done the exact use case you've requested. I recommend you look at the following blog post describing the Drupal States API, including newer additions made to that API for ORing and XORing conditions:

http://www.metaltoad.com/blog/drupal-7-form-api-using-states-multiple-conditionals-and-or-and-xor

For simple multiple-condition clauses the States API follows this convention:

'[field-selector]' => array('[evaluate]'),
'[field-selector2]' => array('[evaluate]'),

For more complex conditional logic you can write the following array structure:

array('[field-selector]' => array('[evaluate]')),
'[OR/XOR]',
array('[field-selector2]' => array('[evaluate]')),

Be aware that the version of jQuery being used on your site could be important for these conditionals to work (again see the blog post).

If the above does not meet your needs I recommend you look at the Field Conditional States module:

The working states that are supported by FCS are:

  • visible / invisible - Hides/Shows a form element
  • enabled / disabled - Grays out a form element
  • required / optional - Makes a form element required to be able to submit the form
  • checked / unchecked - Checks/Unchecks a checkbox (of course only applicable to checkboxes)

Fields to which these states will be applied, are called target fields or controled fields.

To trigger these states there is a different set of trigger states (not to be confused with the states described above) that will trigger a state when applied to a control field:

  • empty - Triggers when the control field is empty
  • filled - Triggers when the control field is not empty
  • checked - Triggers when the control field is checked
  • unchecked - Triggers when the control field is not checked
  • value - Triggers when the control field's value is equal to a given string
  • !value - Triggers when the control field's value is NOT equal to a given string

All of them are supported as long as they make any sense for the widget of the used control field. For example it doesn't make any sense to wait for a text field to be checked or for a checkbox to have the value "Hello World". So most (if not all) fields will only use a subset of these trigger states.

Note: that the states api and the field condition module are clientside modules -- they don't enforce the validation on the server. You'll likely also want to use a custom form validation function to assure that only 1 field is provided upon form submission.

  • Very interesting and certainly a valid solution, but not so good for working with horizontal tabs. Maybe I need a rethink. Thank you. – MrPaulDriver Oct 1 '14 at 8:10
  • no problem, in further questions you may want to provide further details such as "i'm trying to not show certain tabs in my form(s)" ... although some meta programming should be able to hide a tab group if all the conditions in the above examples are met. It just sounds like sufficient javascript-fu is needed. I mean an html form is an html form and its just logic. "tabs" are simply a visual tweak usually achieved with JS and CSS. – tenken Oct 1 '14 at 20:52
  • I have gone with your suggestion and flagged my question as answered. I really appreciate your help. – MrPaulDriver Oct 2 '14 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.