Using Rules for Node Validation

Events happening in the community are now at Drupal community events on www.drupal.org.
guypaddock's picture

I have been using Rules a lot lately and I absolutely love it! The one place I felt it was lacking, though, was that it didn't have the ability to intercept node validation, to trigger rules that ensure that a node a user is submitting matches some complex criteria. So, I wrote a module to do just that!

My module is called "Node Validation Rules", and it provides a trigger called "Content is going to be validated" and an action called "Display a validation error". When a triggered rule is added with that trigger, it can execute the action if the node edit should be held up, and the message it provides is displayed to the user. The action can even supply the identifier of the form element that is at fault, and Drupal will highlight it in red, just like for any other form validation error.

I was going to release this as a separate module on Drupal.org, but I figured I'd release it here first to see what you guys think. Should this be a separate module?

The module can be downloaded from http://drupal.org/files/issues/rules_node_validation-take3.zip.

Comments

Forms validation is now included in the newest release of Rules

DCCG's picture

The newest release of rules 6.x-1.1 (2009-08-28) includes a new sub-module called "Rules Forms support (rules_forms)" that effectively enables the use of Rules for validation of any given form. This functionality is now an integral part of rules and found under admin/rules/forms (Administer › Rules › Form events). This is part of several recent additions to rules resulting from work contributed by now co-maintainers klausi and fago in relation to Google Summer of Code 2009 (GSoC 2009).

Your module is great work and thank you for sharing it. While limited to validation of just node forms, your implementation looks to be a solid solution that does not conflict with the new rules_forms, which has a few limitations itself. Both modules are certainly worth trying out for anyone of the opinion that the more rules from which to choose the better.

Very very very cool. It did

capellic's picture

Very very very cool.

It did take me a second to figure out how to get access to the ".. is being validated" event. I thought that I would just create a normal Rule, select that the content type had to be foo and then would see the validated event there. But I see that you first have flag a form for validation via the Form Events config page. Check both boxes, save, go to the form you want to add validation to, click on the link at the top of the form and then you can make rules specific to that form -- very cool.

Form validation with Rules

danimatielo's picture

Hello, all. I have a problem that I've trying to figure out and you might help me with it. The case is the following: I have a content type called "Registering", which a person can start to fill out at a point and continue later. After the content is complete, the user canno't edit it anymore.

To achieve this, I am using Save&Edit (so, when the content is complete, the person just click in "Publish"), and then rules in order to prevent the user to edit it after it has been publish.

The problem is that I need to validate the form, but only when it is published and not when it is saved. In this sense, I understand I cannot use the "required field" configuration from the core, because then I cannot Save without the required fields.

So, my doubt is: is it possible to use rules to validate fields only when it is going to be published?

If yes, how could this be done?

Thank you, I appreciate your help,

best,

Daniela

node/edit vs. node/add forms

Rosamunda's picture

I need something different but I think pretty close to what you need Daniela: Is there a way to do stuff only in node/edit forms?
Rules treats node/add and node/edit the same way.
What if I need to unset a specific field inside a node that has already been submitted, but I don´t want it to be changed whan the user enters to node/edit?
Thanks!
Rosamunda

Thanks for the code

OddSim's picture

I was unable to determine if (the grossly complex?) Rules Forms Support could verify a field using regex, but your simple module made it easy. Thanks for sharing!

Why do this module isn't in the list of project?

jolava's picture

It would be great to have this project in the project list of Drupal website.

Thanks!!

estebant's picture

I agree with jolava, this should be in the Drupal project list.

Good job GuyPaddock!

Does it work for D7?

Solariss's picture

Does it work for D7?

hi

delacosta456's picture

please has somebody found how to do this in drupal 7

rules and flags

pinueve's picture

I think is posible to flag the node in rules when certain conditions are matched, i have a flag machine name "block_edit", then in a custom module do somethink like this, this function does hide the EDIT button:

function YOURMODULE_node_access($node, $op, $account) {
if ($op == 'update') {
$flag='';
$flag = flag_get_flag('block_edit');
if ($flag && $flag->is_flagged($node->nid, $GLOBALS['user']->uid)) {
return NODE_ACCESS_DENY;
}}}

you can do it to hide certain fields using:

function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {if ($form_id == 'my_node_form') {
hide($form['my_field']);
}}

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week