Posted by binford2k on August 24, 2009 at 9:52pm
I am trying to write a rule to match an empty field and am not having much luck. I've tried about everything I can think of, including some very inventive regexes, but no luck yet. Anyone know how to accomplish this?
Comments
An approach that works for me
I'm new to Rules myself, and not a pro developer, so it took me a bit of ginger poking around to work this out. There are probably better practices and this approach may have faults that I hope others will illuminate, but it worked for me.
Enable PHP Filter and Devel modules, if you haven't already
You'll need to figure out how to get at the variable that you'll be testing for "emptiness". It's going to be an array element buried deep inside of a $node or $form, depending on which kind of rule you're making. For regular node rules, If you open up the node edit page and click on the "Dev load" tab, you'll be able to drill down into the data structure of the node.
For example, I wanted to find the value for a CCK field called 'field_fee', and Dev load showed me this:
#
field_fee (Array, 1 element)
#
The way you would write this in PHP array notation, I believe (I actually haven't done this for regular rules, just for form validation rules), would be $node->field_fee[0]['value'], and to test to see if it is empty, you could say...
<?phpreturn empty($node->field_fee[0]['value']);
?>
For a form validation rule, it would be $form['nid']['#post']['field_fee'][0]['value']
So, for your rule, add an "Execute custom PHP code" condition and then type the PHP return statement with the "empty($your_variable)" test to determine if your field is indeed empty.
One gotcha I found was in testing emptiness of date-time fields. The date and time are broken into separate array keys and Dev load, for some reason just shows the whole date-time string. Here's what worked for me to test whether a date was provided:
<?phpreturn empty($form['nid']['#post']['field_facility_need_time']['0']['value']['date']) ;
?>
Excelent answer, but to compare two dates, it is the same?
I'm newbie in this things but your answer is excellent. Thanks a lot!!, matching a empty field works very good, I had had this problem for 2 weeks and finally I got the solution :).
Now I trying to use this "Execute custom PHP code" condition to compare two dates (datestamp) in the form, I use this to validate a form when the form is being validated
I had tried with this:
<?phpreturn $form['nid']['#post']['field_task_date_received']['0']['value']['date']) < $form['nid']['#post']['field_task_date_deadline']['0']['value']['date']);
?>
this gives me the following error :
Parse error: parse error in C:\wamp\www\PCS_drupal\sites\all\modules\rules\rules\modules\php.rules.inc(107) : eval()'d code on line 1
and I tried to use this for the separate time field
<?phpreturn $form['nid']['#post']['field_task_date_received']['0']['value']['time']) < $form['nid']['#post']['field_task_date_deadline']['0']['value']['time']);
?>
because as you know are different fields... but it doesn't work
I tried with this too
<?phpreturn strtotime($node->field_task_date_deadline[0]['value']['date'])< strtotime($node->field_task_date_deadline[0]['value']['date']);
?>
and even with
<?phpreturn $node->field_task_date_deadline[0]['value']['date'] < $node->field_task_date_received[0]['value']['date'];
?>
but nothing..... someone has some idea to compare this two fields????
Kind Regards
You can't do a mathametical comparison on strings
Maybe try
<?phpreturn strcmp($first_date_string, $second_date_string);
?>
Finally I found the solution
Finally I found the solution two compare two dates when the form is being validated !!
Thanks to you pdcarto :D !!!!
Event : when you_form is being validated
condition: execute custom PHP
<?php$result = strcmp($form['nid']['#post']['field_date1']['0']['value']['date'],$form['nid']['#post']['field_date2']['0']['value']['date']);
if($result>0) return true;
if($result<0) return false;
if($result==0) return true;
?>
Action: Set a form error!!!
I hope that be the same for time field!!!
Kind Regards!!!!
Similar problem with evaluating content of date field.
I was trying to check for content in a field of type http://drupal.org/project/date in drupal 7.
It returned true both with and without content.
It was hard debugging the problem.. but a tip here: http://drupal.org/node/702252, guided me to (after enabling the dev module and PHP filter), to add a custom temporary field to the user account, and fill / populate it with info from within the rule.
I named the new field "debug_text_field". I used "populate value" action and returned custom PHP with this variable $debug= print_r($account), so the custom "debug_text_field" ended up holding the account array as seen by the Rule module.
This way I was able to write around the date field not evaluating empty when empty. :) ( after a long detour trying to get debugging in firebug and devel to work from within the php eval'd fields in rules)
I solved it by writing a custom php condition. :)
<?phpif (empty($account->field_my_date)) { $is_it_empty=TRUE;} else {$is_it_empty=FALSE;}
return $is_it_empty;
?>
Am I right in assuming that the Rules module lacks support for external field modules? Or maybe rather: the field modules lacks support for the Rules module?