Can we have the ability to add a code snippet (php or jquery) to set the value of the dependent field when a certain condition is true with the dependee.
My use case is like this:
I have the following 4 fields on a form:
field_total_amount - e.g. 10000.00
field_full_duration - e.g. 24 (months)
field_frequency - a drop down list with options: monthly, quarterly, annually, fullterm
field_installment - should be calculated and set based on the values above.
If selected value of field_frequency is 'fullterm' the field_installment should be hidden and set to value of field_total_amount otherwise it should be visible (but not editable) and set to (total_amount)/(full_duration/frequency)
I think this module already has most of functionality to implement this feature.
Comments
Comment #1
chalee CreditAttribution: chalee commentedLooks like the feature I'm requesting here is almost what the dependent_fields module solves, however the module doesn't have D7 version yet.
Anyone with ideas of other approaches I can use at the mean time.
Comment #2
peterpoe CreditAttribution: peterpoe commentedPart of this functionality is already present, though undocumented (I have just committed a fix for a bug that prevented it, so if you want to try download the latest from git).
Here the steps to create a custom jQuery effect when editing fields:
1) Add a new "Effect" to the list supplied by conditional_fields_effects(), by altering the $effects variable:
2) Edit your dependency, select the new effect, and save. You might have to clear caches from the "Performance" page first to see the new option.
3) When editing the fields, conditional_fields.effect.js will now try to execute a function 'EFFECT_NAME', which you can define as any jQuery plugin. So create a new javascript file, include it either through a custom module or a theme, and fill it with the following:
An example: I want the text "LOL!" to be appended before my dependent field any time the dependency is triggered.
1) Create a "lol" module, open lol.module and paste:
3) Create a file lol.js and place it in the module folder. Paste:
4) Activate the module, select "LOL!" as dependency effect, and voilà!
Please note that CF is still in development, so any of the above may change. I will post here any variation. Soon it will be possible to add behaviors when showing content as well, so you can for example render a dependent in a way when the dependency is triggered, and in another when it's triggered.
Comment #3
chalee CreditAttribution: chalee commented@peterpoe: Thanks for the reply. I will try this before end of week. Your instructions look quite straight forward, but I have to learn JQuery first. I suppose JQuery would allow me to pull data from the server (by calling a php function) to populate the dependent field's list options based on the dependee's value. FYI I had implemented this functionality using ajax following examples in 'example' module but would favor your approach if its simpler.
Comment #4
chalee CreditAttribution: chalee commentedI implemented your code in #3 and it worked like charm. Now related to this, I wanted to implement a custom condition. In particular I want an 'Field Value Changed' condition which should happen when user changes selected option on a 'select list' field. I noticed there is a hook_conditional_fields_conditions_alter(&$conditions) which I have tried to use but my code doesn't seem to get executed though the new 'condition' is appearing on the CF admin page. Is it ever possible to monitor the onChange event of a field using this module.
Below is my JQuery code in my MODULE.js file:
Comment #5
peterpoe CreditAttribution: peterpoe commentedI haven't tested, but I think that to implement the new condition you just have to bind a new event to the document, like states.js does:
This if for example how the "visible" condition is implemented:
Comment #6
peterpoe CreditAttribution: peterpoe commentedI mixed up things, sorry. The example in #5 refers to states that can be applied to dependent fields when the dependency is triggered.
If you want to add new "monitoring" functions (called "conditions" in conditional fields), you will have to extend the states.Trigger.states object.
Comment #7
chalee CreditAttribution: chalee commentedI thought so. It would be appreciated if you can give a skeleton code structure/example like you did in #3 above.
Comment #8
peterpoe CreditAttribution: peterpoe commentedI was just doing so by adding two new useful conditions: focused/blurred and touched/untouched. Ended up with this code:
This was done by modifying the module. If you want to add new conditions in a contributed module:
1) Implement MYMODULE_conditional_fields_conditions_alter.
2) Add the following code to javascript when needed:
Be sure to use the latest Conditional Fields from git since I just committed a patch that contains the two new conditions and a fix to make "focused" work.
Comment #9
chalee CreditAttribution: chalee commented@peterpoe: Thanks for the code sample above. My use case doesn't seem quite satisfied by this. I implemented a custom condition 'changed' and custom effect 'set_field_value' as below.
In .module file:
In MODULE_NAME.js filed
I have noted that when I use the condition and effect above together, the effect code is not executed. but when I use the effect with another condition like 'value' the effect works. But the 'change' event seems to be working (code executed) as evidenced by the alert inside the function.
Below is my full use case.
In my form I have 3 form element as follows:
field_1 [ ] A dropdown select field with fixed 3 options (A, B, C)
field_2 [ ] A dropdown select field with a dynamically populated list
field_3 [ ] A simple textfield
Here is how I want to happen:
1. field_2 should only be visible when option 'B' is selected in field_1
2. When field_2 is visible field_3 should be invisible(hidden)
3. While field_3 is hidden, it should be automatically set with value of whatever is selected in field_2
In other words options A & B in field_1 requires that data be manually entered in field_3 while option B requires data to be selected on field_2 and copied to hidden field_3
Comment #10
peterpoe CreditAttribution: peterpoe commentedI think the easiest solution to your case is just to implement a new state (NOT a new effect or condition) like outlined in #5:
PHP:
MODULE_NAME.js:
(this js above is buggy and might not give exactly what you expect, but it's a start)
Setup the two dependencies. Descriptions should be something like:
1) field_2 is visible when field_1 has value "B".
2) field_3 is hidden and filled with values from field_2 when field_1 has value: "B".
Comment #11
chalee CreditAttribution: chalee commentedThank you for the code. It works as expected but in my use #9 above, field_3 does not just get the value of field_2 once. Every time a new select is made on field_2, that value must be copied to field_3. Which means we need to bind to the 'onchange' event of field_2. I'm trying but failing to implement a custom 'changed' condition which should be true when the value of field_2 is changed. My function is like below:
Comment #12
drupov CreditAttribution: drupov commentedsubscribe
Comment #13
tekken CreditAttribution: tekken commentedI think this is a much needed feature. As far as I see, with this feature Conditional Fields would also replace the Dependent Fields module which is not ported to D7 and seems inactive.
Can we expect any further development on this soon? Like setting value of the dependent field becoming an official/documented of the feature of the Conditional Fields module?
I wasn't able to get the functionality following the instructions in this thread, so in the meantime further instructions or a patch would be helpful.
Comment #14
peterpoe CreditAttribution: peterpoe commentedYes this would be useful... Taking it.
Comment #15
tekken CreditAttribution: tekken commented@peterpoe: Any progress on this issue? Looking forward to it...
Comment #16
chalee CreditAttribution: chalee commentedI'm eagerly waiting for this too.
Comment #17
jaxpax CreditAttribution: jaxpax commented+1
Comment #18
ydnar79 CreditAttribution: ydnar79 commentedIt has been a few months since anything more was said / done in regards to this feature request. I was just curious if there was any further developments?
I see that the Dependent Fields module seems active again.... There was a commit just a few days ago on December 5, 2011. However, there is still no D7 version available. And I highly doubt that there will be since there is an issue pointing back to this post...
In other words, the proverbial ball for D7 functionality of this kind appears to be in Conditional Fields court. So any progress????
Comment #19
tekken CreditAttribution: tekken commentedI've also have been checking every now and then. Too bad there has been no progress on this issue. I'm looking for ways to work around this module now.
Comment #20
Alaa Rihan2 CreditAttribution: Alaa Rihan2 commentedup
Comment #21
Anonymous (not verified) CreditAttribution: Anonymous commentedThis example post 2 no longer works as the code as changed.
I now get an error:
Is there a chance that you update the example?
Comment #22
faite CreditAttribution: faite commentedYeah this would be very useful. it appears none of the other options for changing values of dependees works in any module.
Comment #23
catmurray CreditAttribution: catmurray commentedThanks for this module! Has there been any progress on the ability to set the value of the dependent field?
Comment #24
jdoris CreditAttribution: jdoris commentedI'm looking to do pretty much the same thing. Would love to hear any ideas or examples. Getting pretty bogged down trying to customize my own module using #states... ugh.
Meanwhile, I can't seem to even get my dependee (a popup date field) to trigger my dependent (an autocomplete field). Thanks.
Comment #26
dqdHas been committed for 8.x and above but forgotten to be set as "fixed". Apart from that:
Thanks for the report and all the efforts in here. But due to inactivity in this issue for some years and because of the upcoming EOL of Drupal 7, I will close this issue on the way by cleaning up the issue queue.
Feel free to re-open as "Needs review" if you found a solution or have a patch to be reported to help others on this outdated version.
If you think this issue or missing feature should be addressed in a newer Drupal core 8 and above compatible version of this project, then please file a new issue to the latest dev.
Comment #27
dqd