Posted by dddbbb on July 1, 2010 at 11:31am
Hi folks.
I've just started out using the Rules module and it seems very cool/powerful. Initially I'm planning to use it to handle form validation.
My question:
Using textual comparison, how do you bring in the content of a particular field? I have element IDs switched on and am aware of the ID I need to use but still I'm having troubles. I need to do something like this:
--
Two texts to compare: *
the text that's been entered into the body field - body_field[body]
the string I'm checking for
--
Can someone point me in the right direction? Initially it would be nice to just get a direct comparison working, then I can start looking at regex for more nifty validation.
Cheers.

Comments
There's always PHP
If you can't get the built in features of Rules working, you can always use the PHP code functionality to get whatever you're looking for. With the Devel module and its "Devel" tab on every node's page, you can craft some PHP really easily. You can get the body via PHP using $node->body and then use whatever php string functions you want to compare it, such as strstr() or preg_match()
Thomas Hansen
www.ThomasHansen.me
Set form error or do something else?
Hi!
In your question it seems that you are using Rules Form Support. This is useful when you want to set form error messages, but if you don't have to interrupt the submission it is overkill – if you want to (say) redirect to a certain page once the node is saved, you can do this easier by using standard Rules.
Assuming that you want to use Rules Form Support, what you have to do is probably what thomas4019 suggests – to use PHP to check if a given string exists in the body field. There are standard rule conditions for checking the content of an entire field, but not for parts of it. (There might be a module allowing this condition check, but I haven't seen it.)
It is rather tricky to find the data in forms, and I am by no means an expert on it, but I think the following expression will give you the content of the body field:
$form['body_field']['body']['#value']. The way to do the condition check is the to use a custom PHP condition.If you want to dive deeper into the content of the form, I recommend running a
dpm($form);in a custom PHP action.Good luck!
//Johan Falk, NodeOne, Sweden
Edit: You'll have to activate the PHP filter to do any custom PHP code, and you'll have to download and install Devel to use the dpm function.
Ok, cool. But what about Regex and element IDs?
Hi, thanks for your reply (and you thomas4019).
I understand that this can be done with PHP providing that I know how to bring in the value of the field in question - sounds like it'll be fun trying to pin this down. I already have Devel installed so I will persevere with this.
Back to Rules Form Support and the notion of 'standard rule conditions for checking the content of an entire filed' - surely this is what the Regex check box is for? Using regex, I could check to see if a string exists within another string.
Or is the problem that I can't bring in the form field's value using the 'standard rule conditions'? This seems odd because the 'DO' part of the triggered rule seems to accept an element ID quite happily. Surely the same ID could be used to grab the value out of that element for the conditional logic?
Success with custom PHP
I've just had some success with the suggested custom PHP method that you suggested. Many thanks for your help.
I'd still really like to see better use of element IDs in the conditions of Rules Form Support. Hopefully we'll see something in a future release.
Thanks again.
Spoke too soon
While I have had some success, as soon as I try to use the value from a field other than the body things seem to stop working.
Can you tell me how I might reference a field with the following element ID?
field_oembed_url[0]
I've tried
$form['field_oembed_url'][0]['#value'](trying to copy the format of your body field example) but it doesn't seem to work. I've also tried$form->field_oembed_url[0]['value'];but no joy.Devel seems to provide clues as to an object's type and if it is an array, how many elements it has. Beyond that I can't see how I'd use it to solve the problem I'm having.
All sorted now
I wasn't looking closely enough at what info Devel was offering. The row that Devel was showing for the CCK field in question could be expanded further when clicked on to reveal the whole array structure. I was missing 1 element.
What I was after:
$form['field_oembed_url'][0]['url']['#value']Now I'm cooking on gas. Thanks for all your help.
Great!
Glad to hear that things are working!
Regarding regexp, I'm afraid that you know more than I do. (I haven't payed attention to the regexp checkbox -- but it surely sounds like it should be used for the purpose you describe.)
I can only agree that it would be nice to have a smoother way to access the content in forms. Tokes is so easy to work with that you wish (and expect!) everything else to be just as smooth. And one day it will be!
Cheers,
//Johan Falk, NodeOne, Sweden