6

I am trying to use webforms with the Rules module.

How can I compare data with the values submitted in a webform ?

For example, if the webform has a yes/no radio button, how can check the submitted value is yes?

When adding a new condition, I choose:

  • Data Comparison
  • Data selector *
  • Data (the submitted webform value)

I'm not sure how to access a particular form_key to check its value. I feel like Data should have more options to choose from.

1

At a minimum, make sure you have the Webform Rules module enabled. Some details about it (from its project page):

This module adds rules integration on webforms.

Did you ever want to react on webform submission using rules but couldn't find the proper event?

Webform Rules makes it possible to catch webform submissions by rules and do whatever you'd like to do with it (meaning: do whatever rules let you do with it).

You may also want to have a look at feature request "Expose submission data to rules". Here is a summary of it:

Right now, submission data is not exposed to rules except by way of token replacement or custom php actions. This patch exposes submission data in a "first-class" way.

Issue status is "Needs work" ... not an "easy" issue.

Time for a work around ...

The very same issue contains a comment with a workaround (it's genious I think, because it's so simple), here is a relevant quote from it:

You can initialize a rules string variable with the tokens. If you need integers or booleans you can subsequently convert it to whatever and then use it to load nodes or whatever.

A picture is worth more then 1000 words, checkout the screenprint included in that comment.

... and a demo

Consider this rule (in Rules export format):

{ "rules_validate_a_component_of_a_submitted_webform" : {
    "LABEL" : "Validate a component of a submitted webform",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional", "webform_rules" ],
    "ON" : { "webform_rules_submit" : [] },
    "IF" : [
      { "NOT user_has_role" : { "account" : [ "user" ], "roles" : { "value" : { "2" : "2" } } } }
    ],
    "DO" : [
      { "drupal_message" : { "message" : "The webform component submitted by [data:contact_us_first_name-value] (= [data:contact_us_first_name-title]) [data:contact_us_last_name-value] (= [data:contact_us_last_name-title]) contains a field labeled \u0022[data:contact_us_subject-title]\u0022 which has value \u0022[data:contact_us_subject-value]\u0022 and another field \u0022[data:contact_us_message-title]\u0022 which has value \u0022[data:contact_us_message-value-raw]\u0022." } },
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "[data:contact_us_message-value-raw]" },
          "PROVIDE" : { "variable_added" : { "submitted_form_component_value" : "Submitted form component value" } }
        }
      },
      { "CONDITIONAL" : [
          {
            "IF" : { "text_matches" : { "text" : [ "submitted-form-component-value" ], "match" : "mail.ru" } },
            "DO" : [
              { "drupal_message" : {
                  "message" : "Stop and listen! ... One of the form components of the form you just submitted (= \u0022[submitted-form-component-value:value]\u0022), contains some value that is not allowed around here ...",
                  "type" : "error"
                }
              }
            ]
          }
        ]
      }
    ]
  }
}

Some details about the above rule:

  • Rules Event: After a webform has been submitted
  • Rules Conditions: User does NOT have role "authenticated" (i.e: an anonymous user submitted the form).
  • Rules Actions:

    • Show an informal message on the site (this is just for QA-purposes, omit this action after you're convinced it works).
    • Add a variable (this is the clue of it all ...), named submitted_form_component_value and with label "Submitted form component value", and with value [data:contact_us_message-value-raw] (the form component we want to validate in subsequent Rules Actions).
    • Use the Conditional Rules module to perform some text comparison. In this case it checks if it contains mail.ru. If so, then perform another Rules Action like "Show an error message on the site". Obviously, at this point you could make any Rules magic happen ("block IP address" seems appropriate).

Be aware: as per the Rules Event we used here, the Webform has been submitted (and so far I've not found a more appropriate event like "Before submitting a Webform").

Showtime

With the above rule enabled, I submitted a webform. This is the message I got (as per the first Rules Action):

The webform component submitted by Pierre (= First name:) Vriens (= Last name:) contains a field labeled "Subject:" which has value " This is a demo of a validation of a webform component using Rules ... " and another field "Message:" which has value "Bonjour! Guess what happens AFTER I submit a webform with a form component containing a string like mail.ru ? The bets are open ...".

And sure enough, this is the error message that follows it right away (as per the last Rules Action):

Stop and listen! ... One of the form components of the form you just submitted (= "Bonjour! Guess what happens AFTER I submit a webform with a form component containing a string like mail.ru ? The bets are open ..."), contains some value that is not allowed around here ...

PS: In this example I'm using the Conditional Rules module, though you could also rework the Rules Action to performing Rules Components (in which you add the appropriate Rules Conditions), so that you don't need this extra module. For more details on that, refer to the answer to "What's the alternative to using the Conditional Rules module?".

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.