Walk-through example: creating rule for Form Validation Based on a CCK Date field

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

I had to play with Rules for forms today so I thought I'd write up the details. Here's an example of a rules setup to handle checking a cck date field when a node is being created.

I have a content type called Offer which has a cck field called offer_date (identified as field_offer_date).
The required behavior is that if the "to" portion of the offer date is in the past - the form validation should fail and an error should be shown.

1) we have to enable form events for the node/add/offer form. Goto admin/rules/form and click "enable event activation messages on forms"

2) Next we activate rules form events for our form. Go to node/add/offer and click on the "Activate events for offer_node_form" link

3) we can now un-check the "enable event activation messages on forms" checkbox if you only have one form you need to work with.

4) Now we create a new triggered rule - admin/rules/trigger/add

5) give the rule a name - like "No offers requiring Time Machines"

6) for the event select "Offer Node Form is Being Validated" then add in categories if you like and click on the "save changes" button.

7) Now we add a condition - select "Rules - Numeric Comparison" and click next

8) In the "Number 1" textbox we type the following:

<?php
echo (strtotime($form_state['values']['field_offer_date'][0]['value2']))
?>

9) choose "less than" as the operation

10) for "Number 2" enter the following :

<?php
echo (time())
?>
and then click save

The php statement in step 8 takes the entered 'to' date from our date field and converts it into a timestamp - the php in step 10 does the same for the current time - and the comparison checks whether the "to" date is less than the current time. If we wanted to use the "from" date for comparision - just change the last part of the line in step 8 from ['value2'] to ['value'].

11) Almost Done - Now under the "DO" section click on "add an action" and select "Rules Forms - Set a Form Error" and click next

12) Fill out the label by adding in the field element we are setting the error for 'field_offer_date'

13) Under Form Element ID: Type the form element - 'field_offer_date'

14) Fill out the message with something meaningful like: "Hey unless our customers are Captain Kirk - they will never be able to use this offer - please choose an ending date that is in the future"

15) Click save and we're done!

To find out the form element id's you can check the 'display form element id's' checkbox on the rules forms settings page.

Troubleshooting the rule evaluation took me a good couple of hours and I hope this walkthru will save others some of that time.

Comments

This might be useful to me. I

Steel Rat's picture

This might be useful to me. I have setup a Purchase Order form, and need to validate based on Status and Date. If the status is a certain value, then one or more date fields need to be completed. Likewise if a date field is filled out, the status needs to reflect the change.

Thanks very much for the little walk-through!

Stuck at 1)

benjarlett's picture

Is this for Rules 2? I'm guessing not cos I can't get past 1).. there's no admin/rules/form

this doesn't work for me

lineyw's picture

this doesn't work for me either - even when i add an 's' to make it forms...i must be missing something easy here?

admin/rules/form(s)

tannerg's picture

add an s :)

admin/rules/forms

php evaluation or form_state tokens not available

dbassendine's picture

This isn't working for me with Rules 1.4, when I use a textual comparison to check two fields are the same as each other. I dug into the rules_condition_text_compare function (rules.rules.inc) and it seems this comparison function isn't receiving the output of the php - ie. the php isn't being executed. This is probably a good thing overall as I'm sure it could be a security risk - but is does mean this validation functionality isn't now working (at least for me).

I also tried to debug the missing form and form_state tokens, with limited success. It seems to me a key piece of architecture is missing for this to work - see http://drupal.org/node/818402#comment-4898168

I'd be glad to be wrong though - let me know if there's an alternative approach.

Thanks, David

Rules

Group organizers

Group categories

Categories

Group notifications

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