Compatibilty with date module
threexk - June 9, 2008 - 18:28
| Project: | Conditional Fields |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Description
Date module select lists and text fields are required even if they are hidden (their controlling value is not selected).
I believe the problem stems from how a Date field is specified as required:
Results of print_r($form['field_birthdate']):
Array
(
[#tree] => 1
[#weight] => 0
[#validate] => Array
(
[date_widget_validate] => Array
(
)
)
[0] => Array
(
[#type] => date_combo
[#field] => Array
(
[field_name] => field_birth_date
[type] => date
[required] => 1
...snip...With this there are two issues:
- Conditional Fields looks for '#required', but not 'required'
- Conditional Fields uses element_children() and so does not look at #field property children.
Should the Date module should change to represent required in a normal way? Or should Conditional Fields accommodate the Date module?

#1
After further research, the Date module does use the #required property for all its form elements. However, it is done by a #process function, which is called after hook_form_alter() where Conditional Fields looks for #required.
At the hook_form_alter() stage when Conditional Fields runs, a date field is sort of a super field that can consist of multiple normal fields; at the #process stage it expands the super field into all its constituent fields.
I found this diagram really helpful for visualizing the sequence: http://drupal.org/node/165104
I am now wondering if Conditional Fields's hook_form_alter() code should instead be executed at the #afterbuild stage, so it deals with the final, processed form (hook_form_alter() could register a #afterbuild callback.) Will need to think about it more...
#2
I hereby offer a $5 bounty to anyone who can fix this issue within two months. Payment would be via PayPal.
The money probably is not commensurate with the work, but perhaps it will provide some extra motivation to someone who was already thinking about fixing it.
#3
Postponing all compatibility with non-core cck modules to future releases.
#4
Please try the latest releases of both modules and update this issue.
#5
Please report here any incompatibility with date module.
Please note that work on compatibility issues with non-core cck modules is postponed after the release of Conditional Fields 1.0 (but patches are welcome of course).
#6
I'm still seeing this behavior with the Date and Location modules. Both field types have nested fields with their own #required attributes, but
conditional_fields_node_editing_formdoesn't seem to recurse.I should note that the date and location fields are being used within field groups, and it is the field group that has the conditional field setting (i.e. when another field has the right value, the field group becomes visible).
#7
Using Conditional Fields 6.1-dev (Aug 15 2009) and Date 6-2.3 Initial testing is showing that i am able to set a date field as a conditional field that is properly triggered. I tried saving the field triggered and not triggered and so far is appearing to work properly.
Will test further and will report of any strange behavior.
#8
If the Date field is required I am getting an error message saying the date field is required even though the date field is not triggered and thus is unavailable.
If the date field is not required then it looks like it is working properly - at least thus far in my testing...
#9
I'm willing to make a financial contribution to getting the this issue fixed and the date module compatible with conditional fields. Let me know if your interested.
#10
I've done some work on this. I have not extensively test this, so please use with care. I think it should for the most part work however.
#11
Small change
#12
Missed the returns in the after build. They now return the form, instead of nothing.
#13
Last patch has a typo on both _form_validate() lines which causes the required checking to not work. #conditional_date_fields should be #conditional_controlled_fields.
Updated patch attached.
#14
I've done a bit more testing, and I think this is working quite well now with the last patch. It doesn't seem to affect anything else, and appears to make date fields work as expected.
The patch itself could probably be re-factored a little bit to reduce duplicate code and checking of conditions, but I don't quite have the time to do that at the moment.
#15
Sorry for so many unanswered posts and renditions of this I believe this one is the right patch now. :) The last one would work, this one just cleans up a variable that was made which was never used.
PATCH SUMMARY
After build callback (Was form alter).
Validation callback
Having written that, I'm not 100% sure on the logic behind needing to re-validate the date fields manually. I know as I was putting this patch together there was a reason for it, but now that it's finished, I might need to recheck if that is actually required.
#16
Ok, so I just checked what I wrote above, and it turns out I was right, the logic is wrong. Here is what I hopefully think will be the last patch. :)
SUMMARY
#17
Realized that XOR logic was a bit miffed.
Corrected.