Conditionals are now supported in Webform 4 (yay!) but there are only two actions available to a condition: 'show' and 'hide'. It would be great if I can make a field optional or required based on a condition.

Use case: If I have a contact form with a "Call me back" check box, the phone number should be required if "Call me back" is checked.

PS. This issue can be considered a duplicate of #213352: "Mandatory If..." (conditional requirements), but that was 5 years ago.

Comments

sylvain lavielle’s picture

Issue summary: View changes

same issue here

danchadwick’s picture

Status: Active » Closed (works as designed)

Remember that required fields that are hidden are no-longer required. So in your use case, use the CALL ME BACK checkbox to show the required PHONE NUMBER field.

Please re-open if there is a more compelling use case that the above doesn't address.

sylvain lavielle’s picture

Status: Closed (works as designed) » Active

Hello Dan.

You're right, but in my case (and I assume in Mark's case too) the phone number still providable by users anyhow : The phone number field should never be hidden. In one case, its mandatory in another its optional, but hiding it doesn't suit my case.

marcvangend’s picture

I completely agree with sylvain. Hidden fields must not be required, but that doesn't mean that not-required fields must be hidden.

adanielyan’s picture

I think the workaround could be creating two phone number fields, one required and one not required. When the "Call me back" checkbox is unchecked the not required field will be shown and the required one will be hidden. When the checkbox is checked the required phone number field will be shown and the not required one will be hidden.

sylvain lavielle’s picture

Yes certainly but it's quite dirty :)

sylvain lavielle’s picture

The way i'm dealing with for now is by adding a custom #validate function to the form webform to check the case backend side and a JQuery script for applying "required" display on the needed fields on the frontend side. That works, but it would be so great if webform conditionals would be able to add two more actions ("set mandatory" & "set optional") to the classic "Hide" and "Show" in conditions.

danchadwick’s picture

Seems pretty reasonable. Patches welcome. Remember there is a server and a jQuery part.

sylvain lavielle’s picture

Status: Active » Needs work

not a piece of cake though :) ...

danchadwick’s picture

Status: Needs work » Active

Active is the correct status as there currently is not patch the "needs work". :) Doesn't matter much, but I sometimes search for "needs work" issues to see if I can fix the patch.

danchadwick’s picture

Working on this. For the record, a webform-related module can respond to the insertion and removal of the required span with something like (just an example -- you wouldn't want this):

  Drupal.behaviors.your_behavior_name = {
    attach: function(context, settings) {
      $('.form-required').once('your_behavior_name').parent().css('background-color', 'red');
    },
    detach: function(context, settings) {
      $('.form-required').parent().css('background-color', 'white');
    }
  };
danchadwick’s picture

Here's a patch for review.

  1. Defines a new action, "required".
  2. Generates a map of required status while conditionals are being executed on the server.
  3. Queries that map during submission validation.
  4. In the browser, inserts and removes the standard core required span tag. Note that this feature won't be compatible with themes that override the standard. I'm open for suggestions about how to improve the flexibility.
  5. Drupal.attachBehaviors and detachBehaviors is called before and after removing the required and not-required spans.
danchadwick’s picture

Status: Active » Needs review
danchadwick’s picture

Status: Needs review » Fixed

Committed to 7.x-4.x.

  • DanChadwick committed 6e1c97e on 7.x-4.x
    Issue #1885056 by DanChadwick: Conditional actions: required / optional.
    
danchadwick’s picture

Version: 7.x-4.x-dev » 8.x-4.x-dev
Category: Feature request » Task
Status: Fixed » Patch (to be ported)
marcvangend’s picture

Thanks Dan!

danchadwick’s picture

Testing is welcome. ;)

fenstrat’s picture

Version: 8.x-4.x-dev » 7.x-4.x-dev
Category: Task » Feature request
Status: Patch (to be ported) » Fixed

Committed and pushed to 8.x-4.x. Thanks!

  • fenstrat committed 4fe0671 on 8.x-4.x authored by DanChadwick
    Issue #1885056 by DanChadwick: Conditional actions: required / optional
    

Status: Fixed » Needs work

The last submitted patch, 12: webform-conditional_required-1885056-12.patch, failed testing.

danchadwick’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.