I am using hook_form_alter to add a custom option called CLEAR in addition to SHOW and HIDE. You can see this in my image at this link.

However, the two default options also have callbacks which define their functionality. And, that's my problem, I can't find how to define similar functionality for my custom defined option.

Can you please give me some direction about how to do this?

Comments

quicksketch’s picture

Title: Adding extra conditionals » Support extra conditional actions besides hide and show (i.e. "clear" or "set value")
Category: support » feature

Hi there! Currently this functionality is not supported (unfortunately). I'd like to see this added to the module through the API, even if it's not a built-in option in the core module. To add this, we need to enhance the Drupal.webform.conditionalCheck() function in the webform.js file. Right now you can see that it's hard-coded in there whether or not we hide/show a component:

      // Flip the result of the action is to hide.
      if (ruleGroup['action'] == 'hide') {
        showComponent = !conditionalResult;
      }
      else {
        showComponent = conditionalResult;
      }

      if (showComponent) {
        $('#' + ruleGroup['target']).find('input.webform-conditional-disabled').removeAttr('disabled').removeClass('webform-conditional-disabled').end().show();
      }
      else {
        $('#' + ruleGroup['target']).find('input:not(:disabled)').attr('disabled', true).addClass('webform-conditional-disabled').end().hide();
      }

What we need to do here is make this section pluggable. I think it would be logical for us to fire an event here, such as finding the parent "form" tag and then triggering a custom event:

$form.trigger('webformCondition', { ruleGroup: ruleGroup, conditionalResult: conditionalResult });

Then any module could respond to this event (and Webform would respond to it itself also for show/hide):

$form.bind('webformCondition.hideShow', function(e) {
  var ruleGroup = e.data.ruleGroup;
  if (ruleGroup.action === 'show') {
    $('#' + ruleGroup['target']).find('input.webform-conditional-disabled').removeAttr('disabled').removeClass('webform-conditional-disabled').end().show();
  }
  else if (ruleGroup.action === 'hide') {
    $('#' + ruleGroup['target']).find('input:not(:disabled)').attr('disabled', true).addClass('webform-conditional-disabled').end().hide();
  }
});

This code probably wouldn't work without modficiation, I'm just dumping my ideas out on how this should probably work. I'm moving this to a feature request because I think it would be a nice enhancement to our API.

quicksketch’s picture

I occurred to me the other day that the conditional system could be used to set custom validation messages. That would drastically improve users ability to set rules around validation and would almost entirely eliminate the need for the Webform Validation module.

quicksketch’s picture

quicksketch’s picture

Issue summary: View changes

added image link

danchadwick’s picture