1

I've created a content type 'H'. The following rule (using the Rules module) has been built to prevent the node from being saved/updated if the field 'a' is larger than 5.

Event: Before saving content of type 'H'.

Condition: Data Comparison - Node field 'a' is larger than 5.

Action: Show a message on the site - Message type: Error.

Action: Go back to the front page.

It will show the error message if the condition is TRUE. However, the node is still being saved/updated.

Is it possible to control this without programming?

2 Answers 2

1

The following rule (using the Rules module) has been built to prevent the node from being saved/updated if the field 'a' is larger than 5.

Rules is absolutely the wrong place to be doing this. Once the save has been triggered by Drupal core, you can't stop that from happening. You need to prevent the invalid data from being saved in the first place.

This kind of validation needs to be done on the front end, when the user provides input. By the time Rules gets a chance to see this input data, the node IS ALREADY IN THE PROCESS OF BEING SAVED. At this point, you can't stop the save.

If you want to restrict the value of a field or a form element, the proper way to do that is with a field validation function or a form validation function. Yes, you will need to write code to do this. There may be other contributed modules that allow you to set constraints on form input values or field input values. Rules is not one of them.

0

I'm not aware of a way to do what you're asking for by only using the Rules module ... And the (old) issue about "Prevent Node from being saved" (with confusing status "Closed (fixed)" ...) somehow seems to confirm you'd need "a bit more" ...

You can try to make it work by ALSO using the Conditional Rules module for this. Some more details about this module (from its project page):

... provides Rules plugins for conditionally altering execution flow directly inside a group of actions.

Here is a screenprint of a rule with an action that uses Conditional Rules (though only with an "if"-part here, no "else"):

enter image description here

1
  • Thank you. I use the 'Conditional Rules' to build the rules. Actually I want to do is that ==> 1. The user create a node 'H' and input a decimal field 'a', 2. After clicking the 'save' button (but before saving the node), the following rule will be checked. Rule: If 'a'>5, print error and quit, otherwise save the node 'H'. However, the node was saved when the error is shown. In other words, it will save the node if the condition is FALSE when the Event = 'Before saving the content'.
    – mms
    Nov 25, 2015 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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