If the Rules action "Set form error" is given the ID of a CCK field (i.e. something like "group_somegroup[field_somefield][0]"), the form error message appears correctly, but the field is not highlighted in red.

The bug appears to be in rules_forms_action_set_error(). The implementation assumes that the field name to pass to form_set_error() is simply the result of imploding the '#parents' array, but this doesn't work for CCK fields because they have nested form elements. For example, the proper name to pass form_set_error() for a CCK text field would be something like "group_somegroup[field_somefield][0][value".

CCK gets around this problem by adding an extra "_error_element" array to the element's form state. The value of the '#value' key in that array provides the full field name to pass to form_set_error.

Comments

guypaddock’s picture

A temporary workaround to the issue is to add "[value]" to the end of the form ID provided to the "Set form error" action (so "group_somegroup[field_somefield][0]" becomes "group_somegroup[field_somefield][0][value]").

guypaddock’s picture

Status: Active » Needs review
StatusFileSize
new673 bytes

The attached patch appears to correct this issue.

Status: Needs review » Needs work

The last submitted patch, rules_forms_cck_form_set_error.patch, failed testing.

guypaddock’s picture

Status: Needs work » Needs review
StatusFileSize
new697 bytes

Re-submit with patch created from the root folder of the rules module.

klausi’s picture

Version: 6.x-1.2 » 6.x-1.x-dev
StatusFileSize
new1.51 KB

Hm, I don't think this is the right way to go. The problem is that the form element IDs are not displayed correctly in the display form element IDs mode. Here is a patch that moves the addition of these IDs to the after build handler, where the CCK fieldgroup stuff is already done. Then we have the correct IDs on the form, with [value] in the case of CCK.

Inspired by fago @drupalcon sf

robby.smith’s picture

subscribing

Bilmar’s picture

Hello,

The patch at #5 applied smoothly and I made a screencast of the difference in element ID info on the node edit page before and after applying the patch (very different!) Are all the element id info shown in the screencast suppose to be showing?
http://www.screencast.com/users/trupal218/folders/Jing/media/e78d6c62-e3...

Screencast includes:
- cck text field select list
- imagefield cck field
- cck text field checkboxes
- cck content taxonomy field checkboxes

I tested by creating a rule.
Triggered event: Profile node is being validated
Condition: Form element has value
Action: Set form error

I used field_example[value] as the element id as indicated on the node edit page after patch applied. Saving the node with value selected in condition sets the form error with message.

Without the element ids that show due to the patch, I wouldn't have known how to make this rule work. I hope this is something that can get committed after more review to make it easier for those using rules forms. Thanks so much!

Regards

fago’s picture

#5 makes a lot of sense to me as it ensures that every element available during validate+submit is shown - this is as during the form processing form elements might be expanded to multiple elements. Validate+submit always run "after-build" however the "form is being built" event not - what might generate confusion as those *new* values are probably not available there.

fathershawn’s picture

This may explain my lack of form error, added into the comments of #576718: 'Form element has value' fails to match CCK field on build form. The field I'm testing is in a CCK field in a fieldgroup and the rule condition always returns false. I'll try adjusting the id.

fathershawn’s picture

Nope - didn't fix my error.

Changed id from field_workflow to group_status[field_workflow][0][value]

Still doesn't evalutate to true when it should.

YK85’s picture

subscribing

robby.smith’s picture

#5 helped me after many hours of confusion and hair pulling
+1 for adding to help admins using this

klausi’s picture

Status: Needs review » Fixed

Committed #5 for now, not ideal but at least all element IDs are visible now.

nodecode’s picture

I have posted a support request question that was raised a result of this thread. It has to do with Element IDs and why they might not be present during the "form is being built" event.

my support request: http://drupal.org/node/893738

Any input is greatly appreciated.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.