1

In one of my content types I have 1 field 'venue' with a drop down option. If I select 'venue' as 'other' from the drop down, some other fields (say 'venue address' and 'venue name') should become required fields and the node should not be saved or updated if those required fields are empty.

This is the screenshot of the rule I created.

enter image description here

This is my rule in Rules export format:

{ "rules_schedule_program_venue" : {
    "LABEL" : "Schedule Program venue",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_presave--schedule_programs" : { "bundle" : "schedule_programs" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_venue" } },
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_venue_address" } },
      { "data_is" : {
          "data" : [ "node:field-venue" ],
          "op" : "IN",
          "value" : { "value" : { "9" : "9" } }
        }
      },
      { "data_is_empty" : { "data" : [ "node:field-venue-address" ] } },
      { "node_is_of_type" : {
          "node" : [ "node" ],
          "type" : { "value" : { "schedule_programs" : "schedule_programs" } }
        }
      }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "address field is required",
          "type" : "error",
          "repeat" : "0"
        }
      }
    ]
  }
}

Please let me know what Rules Action I need to add to show an error message without updating or saving node.

I tried adding the Show a message on site action, but it is updating or saving the node anyway. I also tried with the Set an error on the form action, but in Data selector there isn't the field venue address.

Any other suggestion using javascript or jquery to achieve this task is also fine.
Can anybody help me in using 'Rules Forms' module in this case?

1

I'm not aware of a way to get this to work using the Rules module. Because even if you use Rules Action "set a message" (of type = error) the node will still be saved. For more details on this, and/or possible work arounds, refer to my answer to "How to add a confirm dialog to node add and edit forms?".

If you really want to get this to work with Rules, a possible work around for your case might be to also use the Conditional Rules module, so that in your Rules Action you could "add a Rules Condition" (yes!) to check which option got selected for "venue". And if it is NOT "other", then within your Rules Action you set some dummy value (like "n.a."?) for "venue address" and "venue name".

Plan B

An alternative solution that you may want to experiment with (to see if it works in your case), is the Conditional Fields module. Excerpt from its project page:

Conditional Fields allows you to manage sets of dependencies between fields. When a field is “dependent”, it will only be available for editing and displayed if the state of the “dependee” field matches the right condition.

When editing a node (or any other entity type that supports fields, like users and categories), the dependent fields are dynamically modified with the States API.

You can, for example, define a custom “Article teaser" field that is shown only if a "Has teaser" checkbox is checked.

  • In Conditional Fields module we can get asterik(*) symbol but we can save the node with empty values in the field. – rks Jul 27 '16 at 9:46
  • ha - I was just writing similar answer, though think possibly drupal.org/project/rules_forms might allow validation (ie provide the actions you are looking for). I believe conditional fields module will also allow you to make a field conditionally required – autopoietic Jul 27 '16 at 9:47
  • @autopoietic : I'd be interested to learn how this can be done with "rules_forms". Can you post an answer to explain that? – Pierre.Vriens Jul 27 '16 at 9:54
  • rules_forms has feature Validate the values of form elements during form validation.. It sounds interesting to me. A lovely module. – Jimmy Ko Jul 27 '16 at 9:59
  • I have not used rules forms before, but I could test and post an answer later for future readers (if you have not got there first :) – autopoietic Jul 27 '16 at 13:06

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.