When activating a dependency that changes the required state of a field, required fields are not respected and the entity can be saved without values.

CommentFileSizeAuthor
#213 conditional_fields-enforce-required-1561272-213.patch9.68 KBlaryn
#209 conditional_fields-conditionally_required-1561272-209.patch9.74 KBNWOM
#208 interdiff_188-208.txt727 bytesp-neyens
#208 conditional_fields-conditionally_required-1561272-208.patch9.85 KBp-neyens
#188 conditional_fields-conditionally_required-1561272-188.patch9.67 KBZekvyrin
#186 conditional_fields-conditionally_required-1561272-186.patch9.67 KBZekvyrin
#181 conditional_fields-conditionally_required-1561272-181.patch2.22 KBSerkanB
#159 conditional_fields-conditionally_required-1561272-159.patch8.7 KBEternalLight
#155 required_conditional_fields_test.tar_.gz3.21 KBultimike
#154 integrate-some-patch.patch34.03 KBkespinosa05@gmail.com
#152 interdiff.txt1.36 KBultimike
#152 conditional_fields-conditionally_required-1561272-152.patch8.26 KBultimike
#143 interdiff-139-143.txt1.3 KBczigor
#143 conditional_fields-conditionally_required-1561272-143.patch7.69 KBczigor
#139 conditional_fields-conditionally_required-1561272-139.patch7.54 KBalanburke
#138 137-3.png7.75 KBpeterpoe
#138 137-2.png6.02 KBpeterpoe
#138 137-1.png16.19 KBpeterpoe
#3 scree4.PNG15.4 KBkaizerking
#3 screen3.PNG14.11 KBkaizerking
#3 scree2.PNG12.81 KBkaizerking
#3 scree1.PNG11.28 KBkaizerking
#8 conditional_fields-required_field_validation-1561272-8.patch2.79 KBarosboro
#18 conditional_fields-required_field_validation-1561272-18.patch2.87 KBarosboro
#30 conditional_fields-required_field_validation-1561272-30.patch3.07 KBvflirt
#32 conditional_fields-required_field_validation-1561272-31.patch3.07 KBvflirt
#38 conditional_fields-required_field_validation-1561272-38.patch3.12 KBkunago
#41 conditional_fields-required_field_validation-1561272-41.patch4.18 KBfreblasty
#52 conditional_fields-required_field_validation-1561272-52.patch4.45 KBvflirt
#63 conditional_fields-required_field_validation-1561272-63.patch4.4 KBvflirt
#74 conditional_fields-required_empty_filled-1561272-74.patch7.13 KB_KurT_
#76 conditional_fields-required_empty_filled-1561272-76.patch7.35 KB_KurT_
#81 conditional_fields-required_empty_filled-1561272-76.patch7.2 KB_KurT_
#82 conditional_fields-required_empty_filled-1561272-82.patch7.2 KB_KurT_
#84 conditional_fields-required_empty_filled-1561272-84.patch6.88 KBAlbert Volkman
#85 conditional_fields-form_error_highlighting-1561272-85.patch7.38 KBSquee3P0
#86 conditional_fields-required_empty_filled-1561272-86.patch7.13 KBthePanz
#87 conditional_fields-required_empty_filled-1561272-87.patch7.13 KBthePanz
#88 conditional_fields-required_empty_filled-1561272-88.patch7.77 KBthePanz
#92 conditional_fields-required_empty_filled-1561272-89.patch7.13 KBthePanz
#93 conditional_fields-required_empty_filled-1561272-93.patch7.13 KBthePanz
#105 Schermafbeelding 2015-10-07 om 10.20.45.png110.51 KBReBa
#106 conditional-fields-dependent-checkbox.png32.24 KBReBa
#113 conditional_fields-required_empty_filled-1561272-113.patch7.28 KBm.lebedev
#115 conditional_fields-required_empty_filled-1561272-114.patch7.27 KBm.lebedev
#118 conditional_fields-required_empty_filled-1561272-118.patch7.2 KBm.lebedev
#124 conditionally_required-1561272-124.patch7.21 KBsumitmadan
#126 conditional_fields-conditionally_required-1561272-126.patch7.47 KBZekvyrin
#136 interdiff-1561272-126-136.txt1.29 KBMustangGB
#136 conditional_fields-1561272-136.patch7.66 KBMustangGB
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

peterpoe’s picture

I cannot reproduce this. Can you detail the field and widget types used, and the dependency settings?

reych9’s picture

I am using two fields:
dependent: entity reference - select list (required)
dependent: text list - selection fields (required)

when I do dependence for the bound field be hidden, it is hidden, but when it is displayed can be saved without value.
I even another rule to make it required and shows two asterisks next to the field, but still require vacuum value and saved
thanks.

kaizerking’s picture

FileSize
15.4 KB
14.11 KB
12.81 KB
11.28 KB

I have same problem
I have 2 fields either of the filed is required,settings
Filed_1, and Field_2 or visible when field_a has value=1
field_1 is required if field_2 has value=0
filed_2 is required if field_1 has value=0
When is save without selecting either one the content is saved without triggering the dependency
Also an error message "field_1 or Field_2 is required " is required
please see attached png files, the operation is complete after screen4 but if i save at screen2 stage it allows to save

reych9’s picture

Priority: Normal » Major

any help??

reych9’s picture

Priority: Major » Critical

It's really important able to use this module in user entities types.

perisdr’s picture

I am also dealing with such a problem.

I have a select list and 3 text fields. All of them are visible. If you select any value in the select list the text fields become required.
But the user can hit save without inputting any values to the boxes.

The weird thing is that I would sworn that it was working a while ago. I am looking to see if any new modules may have caused this but so far no luck.

perisdr’s picture

Ok, I am using the 7.x-3.x-dev version that I believe was released on Tue, 13 Sep 2011 00:13:51 GMT
In that version when you go add a "required" dependecy there is nothing more than the configuration.

Now in the last version I see a lot things where added along whith this informative text:
The Javascript form state that is applied to the dependent field when the condition is met. Note: this has no effect on server-side logic and validation.

That means that the only thing that this does is add just the asterisk. Nothing else. Correct?
So you could say that this is working as designed. I am still curious though as I remember testing that feature before adding it. I will try to use my old version on a clean drupal installation and come back with the results.

EDIT: Still no luck
Some similar issues:
#1372284: Required field indicated but not enforced
#1412538: Dependee required when dependent checked rule is not enforced on profile

arosboro’s picture

Status: Active » Needs review
FileSize
2.79 KB

It wasn't fun, but I made a patch for this. It needs to be reviewed, because I'm not sure what side effects will be from changing:


   // Check if this field's dependencies were triggered.
-  if (conditional_fields_evaluate_dependencies($dependent, $form, $form_state)) {
-    return;
+  $triggered = conditional_fields_evaluate_dependencies($dependent, $form, $form_state);

Any thoughts, comments are welcome :)

bharat83’s picture

Patch not working for me.

hass’s picture

I have several select boxes that depend on one checkbox. This means if the checkbox 'foo' is checked, 4 other select boxes are required. I see the required marker is shown next to the select boxes, but the requirement is not enforced on save. These select boxes have a value of _none, but the form is saved without setting them to any other value. Additional I have a checkbox in the same form that is required, when 'foo' checkbox is checked and a value of a select box is 'bar'. The selectboxes are all core widget type Select list.

None of this conditions work. I can always save without fulfilling the requirements. The module is totally useless. No idea why it's installed on 10K servers.

Sk8erPeter’s picture

@hass: I agree that required fields should not just be indicated, but rather enforced, see the other issue: #1372284: Required field indicated but not enforced, and I also agree that this is a very big problem, because this is one of the module's main tasks.
But instead of simply calling this module useless and cr@p, you could rather help the developer(s) find out the source of the problem, and write a useful patch to be able to mark this issue as resolved and help many site maintainers too.
By the way, this module is useful for other tasks too (e.g. simply toggling the visibility of some fields according to some conditions), its task is not just the one you need it for (enforcing required fields to be filled out). BUT you're right that this issue still remains critical.

koppie’s picture

Priority: Critical » Major

I've marked two other discussions as duplicates of this one:
#1372284: Required field indicated but not enforced
#1412538: Dependee required when dependent checked rule is not enforced on profile

I'm also changing the priority back to "major." The priority flag isn't meant to be used as a bump if your issue is being ignored. Please see https://drupal.org/node/45111. I agree that this issue definitely deserves major priority; it's an important function that does not work, and has received a lot of discussion and multiple patches. Clearly there are a lot of people who think this is important.

I'm going to review the patch in #8 when I have a chance.

koppie’s picture

Status: Needs review » Needs work

I'm afraid the patch doesn't work for me either.

koppie’s picture

I have a workaround. I added a custom module to my own site with the following code:

function first_node_validate($form, &$form_state){
    if ((strpos('incident', $form->type) !== false)) { // Check the content type
        if (($_REQUEST['field_incident_type']['und'][5]) // Check the dependee field
             && (!$_REQUEST['field_incident_security_action']['und'][0]['value'])){ // Check the dependent field
            form_set_error('field_incident_security_action','You must specify a <em>Security Action</em> on the Security tab.');
            $errors = drupal_get_messages();
            foreach ($errors as $type => $id) {
                foreach ($id as $message){
                    // Loop through individual messages, looking for ones to remove or replace
                    if (test_for_invalid_error($message)===FALSE){
                        drupal_set_message($message,$type);
                    } elseif (test_for_invalid_error($message)!==TRUE){
                        drupal_set_message(test_for_invalid_error($message), $type);
                    }
                }
            }
        }
    }
}

It checks for the specific content type, checks to see if the "dependee" field is set, then checks to see if the dependent field is empty. If it is, then it returns an error.

The problem is this is a "one off" solution that only works for a pre-defined content type with a pre-defined field. But I'm hoping someone can take this logic and apply it to a more general solution, and maybe even a patch!

seaneffel’s picture

Issue summary: View changes
Priority: Major » Critical
Status: Needs work » Active

Still busted. I looked at this module last year for a possible solution, but I see this specific problem is still unsolved.

Under no circumstance can I make the module respect the required status of the text fields. Tried setting the dependency on a widget value or on a regular expression. Tried with the target fields set as required or optional. Tried with the dependency state switching between option and required. Tried setting the dependency based on checkboxes and radio buttons. Tried setting dependencies on whether the field is empty or filled. Tried it all.

I can always save the node with no value in the conditionally required fields. The check against the required field state is jacked across the whole module.

If you want to reproduce this, set this up:

Dependee field:
List (text) field with values A and B

Dependent fields:
Text Field A
Text Field B

Dependencies:
When List field reads A, make Text Field A required.
When List field reads B, make Text Field B required.

I'm advocating that this is a critical bug. I say it's critical because the feature says it can conditionally set the required state of a field and it just can't. There are no working patches for this, either.

seaneffel’s picture

Title: Required field is saved without content in user fields. » Conditionally required fields are not required

Oh, and a fancy new title that describes the broad impact of this bug.

hass’s picture

arosboro’s picture

I have found a bug with the form_set_error statement, that was causing validation to fail. I believe this patch will work in most cases.

seaneffel’s picture

You might have a great patch there, but it doesn't address this issue. Applied to 7.x-3.x-dev and the conditionally required fields are still not enforced as required. The little red asterisk still suggests the fields are switched to required, but they are not enforced.

arosboro’s picture

I'm applying this to the non dev branch, and it gives a form error when you try to checkout, maybe you could install devel and come back here with the value of the the $error_key variable and the name attribute of your required field. They should match for form_set_error to trigger.

seaneffel’s picture

@arosboro: I'm trying to get conditionally required fields to enforce their required status, but I can't test that on the current recommended release because conditional fields do not store values (this old thing: https://drupal.org/node/1542706).

The current dev release can store values. Can we get a patch against that one?

seaneffel’s picture

On a side note, my specific goal of setting fields as both required and visible based on the value of another field can be reached with this module: https://drupal.org/project/field-conditional-state

But it can't be reached with this rewrite of the same module because of unstable code (so far): https://drupal.org/project/field_conditional_state

Note the difference in underscores and dashes in the URL.

Not jumping ship on this issue, though. I prefer to use this module because of the higher usage among the Drupal community.

Ndesign’s picture

subscribing to issue

kbasarab’s picture

So looks to me like the patch in #18 gets us almost there. The issue is that the check for:

          if (empty($input_state)) {
            form_set_error($error_key, t('The !title field is required', array('!title' => $dependent['#title'])));
          }

That $input_state variable is not just a string but an array with the structure $array[0]['value'] for a standard text field. This structure changes with fields such as a link field though and becomes $array[0]['url']. We probably need to add some logic to see what the field type is and return that. No error is reported because $input_state is not empty so it doesn't set the error. We need to be checking the value of that or whatever that final attribute is.

er.pushpinderrana’s picture

#18 patch not working as expected. Even after applying this patch it created another issue, now Conditional fields values not saving.

zmove’s picture

#18 doesn't fix the issue.

I'm a little afraid to see a critical issue that is 2 years old. It seems a so basic need to have a required field depending on another field and a module that have more than 30k download still can't do it correctly ?

Did I miss the solution ? Looked in the issue queue and it seems there a lot of issue related to that problem with no working solutions...

The Field Conditional State can be an option for some people, but not all the fields types are compatible with it (in my case, a vat number field, that's not compatible), so the only solution I have is a working conditional fields module.

rooby’s picture

re #26:
Field conditional state does not do required properly either (and I believe it is not something they are looking to add as it is not a part of the drupal core state system that is used).

The reason it hasn't been fixed yet is that it is not trivial to fix and it would seem no one has needed it enough to put the time into fixing it.

I would also argue that this issue shouldn't be marked critical as it is a known issue and doesn't actually break your site. It can lead to problem data though so I understand the concern.

seaneffel’s picture

I think critical is a good choice for a bug in a module called Conditional Fields where the fields that are required by condition are not actually required. It would seem that a central piece of the module's function isn't working right.

zmove’s picture

+1 to #28, It can be considered as critical when a field that should be required is not. Especially when it's one of the main task of a module.

Imagine you need to register professionnal user depending on a required field that doesn't work, it can completely alter the value of your database... definitely a critical problem.

vflirt’s picture

Hi,

a bit better checking for empty fields going trough _field_filter_items() as in field_default_validate().
However with file/image fields the title cannot be taken when setting error but nonetheless there is error set so at least it is working :)

vflirt’s picture

Seeing 1 more problem with reference fields, should be better to use form_state['values'] instead of form_state['input'].

vflirt’s picture

Adding the patch.

chefnelone’s picture

#32 It allows the form to be saved. The message "The xxxx field is required" is shown once the form sent.

I added a Required dependency field to the commerce checkout (billing fields)
form at: http://localhost/example.com/en/checkout/28 [1]
But the form is saved even if the field is empty.

kunago’s picture

@vflirt: As I was able to discover running through the code, this patch works only for such conditions when the dependent is evaluated against a "value", not conditions such as "empty" etc. The "conditional_fields_dependent_validate" function does not seem to be entered otherwise.

vflirt’s picture

@kunago : I am not quite sure I understand what you mean. From what I see looking at function "conditional_fields_form_after_build" it will call "_conditional_fields_element_add_property($dependent_form_field, '#element_validate', 'conditional_fields_dependent_validate', 'append');" no matter the condition "value" or not so "conditional_fields_dependent_validate" will always be called. This patch is calling the proper "empty" function for the field the same way drupal core does it in order to check if it is empty or not. I have taken the patch from comment#18 and addded a couple of lines.

kunago’s picture

@vflirt: I made a few tests and whenever I wanted to make use of your patch, it did not pass through the "conditional_fields_dependent_validate" function unless you set the "value" to check for. I wanted to use "empty" and it did not work. Tried to output whatever message with devel module I placed in the function. If it works for you, it certainly does not work for me.

vflirt’s picture

Hi,
as I described I added small thing to the patch so I cannot take any credit for the whole patch. I want to help you figure out your issue and make this patch even better but I cannot understand your issues. Could you try ti either describe how to setup the fields and conditions or export a feature so I can take a look and understand what is the problem? Also this way other people can also join in and help as this benefits all the community :)

Kind Regards,
Dobromir

kunago’s picture

I suggest one change in the patch. While it works for forcing a field to be required when it is set to be optional, it does not work the other way around, meaning force a field to be optional when it is set to be required.

The modification seems to fix it.

EDIT: This patch only works for my specific case. That leads me to a simple fact that the module does not have the logic very well thought out.

VVS’s picture

Kunago thanks for #38. Patch working, but I do not change value in my date field! Change, but after save node - value set to old. :(

kunago’s picture

@VVS: The module seems to handle well the situation, when a field is optional, set is as required. My use case was the opposite, meaning set a field that is required as optional. In the end I ended up using a very dirty hack in the module to force it behave the way I wanted but it definitely is not a solution.

freblasty’s picture

Attached patch is based upon the changes made in #38 and should fix the problem mentioned in #39.

freblasty’s picture

Status: Active » Needs review

The last submitted patch, 8: conditional_fields-required_field_validation-1561272-8.patch, failed testing.

Status: Needs review » Needs work
Nikdilis’s picture

zmove’s picture

#41 doesn't work. In fact, I don't see the difference with the original conditional fields.

I'm on my user registration form, I have a required dependency. When I check the checkbox that make the other field required, the required red mark appears (on each of the checkboxes instead of the checkboxes label (first bug)) and I can submit my form without filling the required field, it will pass.

So the patch seems to correct.... nothing...

Status: Needs work » Needs review

Status: Needs review » Needs work
vflirt’s picture

Hi,
as I have mentioned before $dependent['#title'] is not always set in conditional_fields_dependent_validate which causes Notice: Undefined index: #title. As form element structure is a bit inconsistent depending on field types is hard to actually figure out the field instance and field info does not have enough info to show proper title. I have added a small work around for such cases which covers only image field in my specific case but would be nice if more people look into it to figure out more general approach to it if needed to go further then $dependent[0].
One idea is to get the field info and then try to figure out the entity info to get the instance and get the title from the instance but considering the different amount of field types and how they handle multiple values that might be quite complicated combined with translations.
Other option could be to descend into element children of dependentrecursively and look for #title but that wold not be quite accurate.

Adding the small difference to patch from #41.

ignat.volosky’s picture

#52 is working for me, but dependent field doesn't highlight, if validation hasn't passed.

mohmmadalabed’s picture

I agree @Svetozarrr #52 is working but without highlight

lex0r’s picture

Patch #52 doesn't work for me. I still don't get any validation...

ilgriso’s picture

Patch #52 doesn't work for me. I still don't get any validation...
This thing is really making me crazy.
I tried the module in a clean installation of drupal (even via simplytest.me site).
If I create a field visible and required by another field: the process seems to work (the field is displayed with the red star), but if I don't insert any value in this field no alert message are shown and the data are correctly insert (without the required - conditional - fields).
Am I doing something wrong?

davewilly’s picture

Fantastic module, but this shortfall makes me a little sad. I guess for the moment a work around is to hook the form submission and manually validate any required fields.

koppie’s picture

Yup. That's exactly what we did.

shaisamuel’s picture

Patch #52 doesn't work for me. The Dependent field (Integer) is required (field setup), and saved even when its empty.
I also added an addition rule to make it require on the same Dependee condition and it does not work.

torekaas’s picture

Hi,
this is my first post...hope to help someone!!
these are the configurations I made to set a field required based on another field value:
field_esito List (text) Select list ...single value not required (YES or NO)
field_data_esito Date Pop-up calendar .....REQUIRED
In Manage dependencies I set this:
Data esito (field_data_esito) Esito (field_esito) Data esito is visible when Esito has value "1".

If field_esito has value NO the content is saved without error.
After selecting the value YES on field_esito the field_data_esito is shown and must be fill with a date value in order to save the content.

I used conditional_fields 7.x-3.0-alpha1

knalstaaf’s picture

Confirming that #52 is not working for my (binary) dependee.

@torekaas: your approach looks similar to the conditional components used in Webform: if it's not shown, it's default requirement is disabled as well. I tried it myself and this seems to work in my case.

  1. I made the dependent field required by default.
  2. Then I made a conditional in which the (binary) dependee must have only one of these values (XOR): 0 in order for the required field to show / be visible.

0 is the "Off" value in my case, so the required field must only be visible when the dependee is unchecked. Mind the XOR-option in the Values input mode field, it won't work with the other options (and, or, not).

That's the only dependency rule that goes in there (no longer need for a "make required" dependency rule). Make sure to clear the cache if no change is visible at first sight.

I'm not sure if this approach serves the whole audience, but I guess some may benefit from it. The issue remains though.

EDIT: I must add this method can cause some issues with Field Collection fields, even when they're not affected by the conditions (!). It's better to use no -required- fields at all in this case - then it will work just fine.

kaizerking’s picture

For now this patch worked for me. I don't know the implications with this patch

vflirt’s picture

There is one small bit in the patch that is missed and cause some tests to fail. Adding that bit.
The patch seems to work quite well for all core fields and their conditions.

smurfxx’s picture

Patch #63 solved partially my needs:
I have a select field and I want to show and make required a text field if I select a certain value.

Without the patch I cannot save the text field value, now it saves but the field doesn't become required.

vflirt’s picture

@smurfxx
I have exactly the same setup :
1 condition select field
1 select field that is not required but when e certain value from the select is selected then the text field becomes required.
Without the patch you can save the form with no issues but with the patch I am not able to save the form and I am shown error message. If you want I can upload screenshots or features with the setup. I have tested that with all core fields and it is working as expected: when a certain value is selected then the field becomes required and you cannot save the form.

smurfxx’s picture

I think that the difference between my setup and your is that for me even the select field is required.

I tried again to patch (I was not sure that I applied well) but I have the same results, the text field can be saved empty.

I applied the patch #62 to the latest dev version.

vflirt’s picture

I have tested with the select (condition) field required and not required and had no issues. I will upload some screenshots and a feature export file for my setup.

vflirt’s picture

Here is a link to the screenshots and exported feature:
https://www.dropbox.com/sh/0xauhj9f2sugqmw/AAAyZ4YwZKJP8SssInHfOs_7a?dl=0

smurfxx’s picture

Thank you very much, I'll try that soon!

EDIT: I don't understand one thing: do I have to apply all the patches to the latest dev version or just last patch? Or can you post a full module that works?

vflirt’s picture

I added copy of the module as well in the dropbox.
version = "7.x-3.0-alpha1+15-dev"
although in the folder the patch is from 52 you can see the code that it has 63 applied. The diff between the 2 patches is only 3 new lines.
No other patches has been applied to the module.

_KurT_’s picture

patch #63 doesn't work for me. Tested on conditions:
"Image is required when Tags is empty."
"Image is required when Tags is filled."
with standard drupal article fields.

smurfxx’s picture

First of all thank you very much for your support and kindness!

I uninstalled old module, deleted file, copied your module and enabled.

I have always the same result, but now I see the difference between my case and your case:
in your case you have a normal select list, in my case I have a checkbox select, I changed the rule from "Insert value from widget" to "All of these values (AND)" and putted the value manually. Now it works!!!!

For some reason if I use the value from widget the dependency desn't work. For me it's good now!

vflirt’s picture

Yep, it does not work for the case you have described for a reason that is core part of the conditional fields module :

Options that need to evaluate if the dependency is triggered only apply if the condition is "Value"

so unfortunately there is a lot more to be done when working with conditions other then "Value" like empty, filled and so on.
It seems that most people work with a select field for condition so using the "Value" condition is what is selected.

This is a 2 step process:
1) check that we have triggered dependencies for the field
2) check what should happen based on those triggered dependencies
this patch covers mostly the second part but is not covering the first one. It does work for it base issues:
when a dependency is triggered and field should be required then it is marked as required.
This patch is work in progress and I agree that fixing both steps is to be expected. If anyone is willing to put more time into adding cases for "empty" and "filled" then that would be great.

_KurT_’s picture

Status: Needs work » Needs review
FileSize
7.13 KB

Add triggering on empty\filled condition, seems like now it works.
Include patch #63

michielkenis’s picture

The patch in #74 seems to work except I get an warning:

Warning: array_values() expects parameter 1 to be array, null given in _field_filter_items() (line 502 of /Users/*/Sites/*/modules/field/field.module).

I traced it back to this line in the patch:

$input_state = drupal_array_get_nested_value($form_state['values'], $dependent['#parents']);

It should return an array, but I got NULL. I'm using an Entity Reference as field.

So I added an extra line right below:

$input_state = (is_null($input_state)) ? array() : $input_state;

Which seems to fix this issue in my case!

_KurT_’s picture

add michielkenis fix

Status: Needs review » Needs work

The last submitted patch, 76: conditional_fields-required_empty_filled-1561272-76.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 76: conditional_fields-required_empty_filled-1561272-76.patch, failed testing.

_KurT_’s picture

_KurT_’s picture

_KurT_’s picture

Status: Needs work » Needs review
Squee3P0’s picture

Copied code from includes/form.inc to fix the field highlighting.

I also commented out the line that changes '_none' to an empty string when doing value comparisons. It makes sense for the empty/!empty checks, but if you try to compare a select list against the the empty value (- None -) it doesn't work since $dependency_values is actually equal to '_none' and not an empty string. Can someone more familiar with this module look at that line and see if it is safe to remove?

thePanz’s picture

Unified code from @Squee3P0:

  • Fixed bug on "checked" conditions for Boolean fields
  • Properly highlight of required field
thePanz’s picture

Corrected "required field" message

thePanz’s picture

Updated error message, following Drupal7 standards ("Every message ends with a period.")

Status: Needs review » Needs work

The last submitted patch, 88: conditional_fields-required_empty_filled-1561272-88.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 88: conditional_fields-required_empty_filled-1561272-88.patch, failed testing.

thePanz’s picture

thePanz’s picture

Rerolled patch

thePanz’s picture

Status: Needs work » Needs review

Frumpus’s picture

#93 still not working for me. I have a checkbox that, when checked, is supposed to be making the body field and an image field required. The asterisks appear, but neither field is enforced. Still.

stevieb’s picture

#93 works for me on a fresh setup

_KurT_’s picture

Status: Needs review » Needs work

Seem like there is a problem with indication if select field is filled, mean it is always filled, but 'none' should be interpreted as not filled. Tested #93 patch on such conditions:
textfield is required when textfield is filled - works fine
textfield is required when select is filled - doesn't work, it is always required
body is required when single checkbox is filled - works fine

Joe Huggans’s picture

I can also confirm that as well as #22 that that this unsupported module which is the old version of Field Conditional States actually works with Drupal commerce line items. The interface is also much more user friendly in my opinion.

http://ftp.drupal.org/files/projects/field-conditional-state-7.x-1.1.tar.gz

bkno’s picture

#93 works for me. Thanks! In my case with fields that are only required+visible when a select is empty.

Anyone experiencing issues with select is empty or filled conditions (possibly other field types also) try checking for the exact value "_none" as the empty value. I found that filled/empty didn't work, but that is a separate bug to this required issue.

_KurT_’s picture

Status: Needs work » Needs review

#93 works for me, set to needs review

Need to add new bug about filled/empty select field, like we said in #98 and #100

sense-design’s picture

Same as #97, a field with checkboxes and each value has a depedency on another field (visible + required). if only one of the checkboxes is checked everything works fine, but more than one is checked the required fields are not required any more but they show up.

sridharan’s picture

Status: Needs review » Needs work
sridharan’s picture

ReBa’s picture

I have following setup:

Checkbox (are you x?)

Checked => required team, required category, required position
Unchecked => optional team, optional category, optional position

With patch from #93, following error occurs:

Notice: Undefined offset: 0 in conditional_fields_evaluate_dependency() (line 1123 of /sites/all/modules/contrib/conditional_fields/conditional_fields.module).
Notice: Undefined offset: 0 in conditional_fields_evaluate_dependency() (line 1123 of /sites/all/modules/contrib/conditional_fields/conditional_fields.module).
      if (!is_array($dependency_values)) {
        $key = current(array_keys((array) current($values)));
        $dependency_values = array(array($key => $options['value'][0][$key])); 
        $temp[][$key] = $values[0][$key];
        $values = $temp;
      }

Adding a dpm($options); before, I get the array as image.

Hoping to provide extra information.

ReBa’s picture

I only get the error above when I'm using multiple fields dependent on a checkbox field.
Dependent - dependee situation

kenorb’s picture

The patch #93 doesn't work as expected, it throws form_set_error for checkbox Text field which is not set as required, but dependee on the other checkbox (has value) which is required.

After some testing and playing with conditional fields (creating them from scratch) it actually start to work it-self, weird. So patch #93 seems to work for me now.

kenorb’s picture

Status: Needs work » Needs review

It was set as 'Needs work' without reason.

kenorb’s picture

Status: Needs review » Reviewed & tested by the community

Based on feedback from #97, #100, #101, #102 the patch from #93 seems to work, everything else could be just a noise, unless somebody has some specific details what exactly needs work.

Here are reproducible steps without patch:

1. Log-in as admin (e.g. https://simplytest.me/project/conditional_fields/7.x-1.x).
2. Enable Conditional Fields module.
3. Create two non-required fields /admin/structure/types/manage/page/fields (Not Required field)

  • A new Test field List(text) as Check boxes for Page content type with options: "First", "Second", "Other" at .
  • An another extra field "Other" as Text field.

4. In Manage Dependencies Tab add two dependencies:

  • Dependent: "Other" text field, Dependee: Test field, The dependent field is visible when dependee has value: (from the widget) "Other"
  • Dependent: "Other" text field, Dependee: Test field, The dependent field is required when dependee has value: (from the widget) "Other"

5. Now submit the Page by typing some Title and selecting Other option in Test field at /node/add/page.

The problem is that the field is shown as required, but the validation doesn't work and allows you to submit the form.

After applying the patch, it works fine, so the field is required on Other selection, and it's not on First or Second selection.

The patch and its fix can be re-tested (following above steps again) in sandbox at: https://simplytest.me/project/conditional_fields/7.x-1.x?patch[]=https:/...

Pepe Roni’s picture

As in #105 I also get the notice messages for each conditionally required field, in my case 18 messages!

kenorb’s picture

Status: Reviewed & tested by the community » Needs review

Needs further review or work as per #105 & #110

itamair’s picture

#93 worked for me so far. I wasted hours to make a required field, un-required on conditional conditions with from alter and validations funct, without succeeding.
This is a super useful (absolutely needed) patch. Should be embedded ASAP.
Tnx to all ;-)

m.lebedev’s picture

Changes:
1) Code style
2) Error Message. Message is replaced by version from the core.

Status: Needs review » Needs work

The last submitted patch, 113: conditional_fields-required_empty_filled-1561272-113.patch, failed testing.

m.lebedev’s picture

I'm sorry, it will be better.
Changes:
1) Error Message.

kenorb’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 115: conditional_fields-required_empty_filled-1561272-114.patch, failed testing.

m.lebedev’s picture

m.lebedev’s picture

Status: Needs work » Needs review
sgurlt’s picture

#118 Works :)

shi99’s picture

I found one issue with patch in #118

If the value is "0" (Zero), then the required field is still ignored.

Here is how I have it set up.

I have two fields:
- Team Score 1
- Team Score 2

I have two conditions:
- If Team Score 1 is filled then Team Score 2 is required
- If Team Score 2 is filled then Team Score 1 is required

All works fine except if the number zero is used.
I set Team Score 1 to have the value 0
Team Score 2 field gets shown as required, but I leave it blank.
Click save and the node saves instead of telling the user that the field is required.

Thanks

icicleking’s picture

Patch #118 works on my local: PH 5.5.29

icicleking’s picture

Patch #118 works but it spits out this error: `Notice: Undefined offset: 0 in conditional_fields_evaluate_dependency() (line 1133 of ...../conditional_fields/conditional_fields.module)`
Here's the if statement and comments containing the erroring line:

// If $dependency_values is not an array, we can only assume that it
      // should map to the first key of the first value of $values.
      if (!is_array($dependency_values)) {
        $key = current(array_keys((array) current($values)));
        $dependency_values = array(array($key => $options['value'][0][$key])); // this is line 1133
        $temp[][$key] = $values[0][$key];
        $values = $temp;
      }

I think this will be displayed to users trying to fill out the form. Even when it successfully saves.

sumitmadan’s picture

I have made a little change in #118 because it was not working in commerce kickstart product variation.

I have changed :

form_error($dependent, t('!name field is required.', array('!name' => $title)));

to

form_error($element, t('!name field is required.', array('!name' => $title)));

Zekvyrin’s picture

#123 notice also happens to me as well

sometimes $values isn't like this

array(
0 => array (
value => ...
)
)

but it is like this:

array(
und => NULL,
)

Zekvyrin’s picture

I've updated the patch fixing the notice mentioned in #123.

Also I added a check to remove an "add more" button (it's similar to how drupal_array_get_nested_value is used in field.default.inc).

TravisJohnston’s picture

The patch fails on the latest 3 dev.

Hunk #1 FAILED at 536.
Hunk #2 succeeded at 678 (offset 6 lines).
Hunk #3 succeeded at 723 (offset 6 lines).
Hunk #4 succeeded at 794 (offset 6 lines).
Hunk #5 FAILED at 1034.
Hunk #6 succeeded at 1057 (offset 7 lines).
Hunk #7 succeeded at 1083 (offset 7 lines).
Hunk #8 succeeded at 1131 (offset 7 lines).

Zekvyrin’s picture

@TravisJohnston:

I tested my patch again in latest dev & on git repo (in case I made a mistake creating the patch).
Both were patched without offsets.

TravisJohnston’s picture

This may be something I have to customize then. The most likely reason it failed for me is because I have another patch that fixes an issue where if you have more than one form on a page and both use conditional fields, only the first form will work.

Thanks for confirming, I'll see if I can manually apply these changes.

Ada Hernandez’s picture

Status: Needs review » Reviewed & tested by the community

It worked for me. Thanks.

giupenni’s picture

Worked for me, thanks.

satroy’s picture

#126 worked perfectly for me :)
Thanks!

shi99’s picture

Patch #126 worked for me except in one case

Making Fields Required

Field 1
Field 2

Dependant: Field 2
Dependee: Field 1
Condition: Field 2 is required when Field 1 is filled

Works fine for the following
Set Field 1 to use the value: 5
Field 2 is shown as required (Red star) and on attempt of saving, it will not save and will tell you it is required.

Does not work for the following
Set Field 1 to use the value: 0
Field 2 is shown as required (Red star) and on attempt of saving, it will save even though it should not.

Making Fields Optional

For conditions on making required fields "Optional" it doesn't seem to work for checkboxes.
Field 1 (checkbox | optional)
Field 2 (text field | required)

Condition:
Field 2 should become optional when Field 1 is checked.

Result:
Field 2 is correctly displayed as optional (no red star) when field one is checked, but on saving the field is still required and will not save.

Hopefully this helps

TravisJohnston’s picture

Just want to add, for those having trouble, that I have that I found that even after this require patch, and the other patch to handle multiple forms on a page, I was still having trouble like the others when there was a mix of visible and required fields and the rules not being applied properly in testing.

I have cases where the same field should show in 5 out of 10 circumstances, and be required for 3 out of the 5, and it would only work for one instance out of them all. The way I was able to finally fix it was to instead of creating a separate dependency for each condition (show if value this, another dependency for show if value that), I grouped all the visible conditions into a single dependency rule and all the required instances in another single dependency rule.

The way to do it is choose the Only one of these values(XOR) and list the term ids or keys for the values you want to validate against on separate lines.

Now everything is working as it's supposed to!

MustangGB’s picture

Status: Reviewed & tested by the community » Needs work

It works for "Form state: Visible", it doesn't work for "Form state: Invisible".

MustangGB’s picture

Status: Needs work » Needs review
FileSize
7.66 KB
1.29 KB

My attempt to address #135, tested with states visible and invisible (!visible), but nothing else.

peterpoe’s picture

Status: Needs review » Needs work

Sorry if I jump in late, but unless I am missing something, I am not even sure what is the bug here.

As stated in the description of the "Form state" option:

The Javascript form state that is applied to the dependent field when the condition is met. Note: this has no effect on server-side logic and validation.

This is by design, since Conditional Fields is based on the States API, which is a purely client-side API.
As a consequence, CF will not make a field (un)required on the basis of the state of the form!

What CF does, during form validation, is: IF the "Condition" is "Value" AND the dependency was NOT triggered, THEN remove the dependent field data from $form_state['values'], which tricks Drupal in thinking that the field was not in the form in the first place, and allows bypassing of any kind of validation, requirement included.

So, if you want a working "Conditionally required field" setup (like the one outlined by kenorb in #109), you just need to:
1) Set the Dependent as a "Required field" in the field settings
2) Create a dependency with "Condition" = "Value" and "Form State" = "Required"
That's it.

With reference to #109: the "Other" field should be set as "Required field" in the field settings, and a single dependency should be created: Dependent: "Other" text field, Dependee: Test field, The dependent field is visible when dependee has value: (from the widget) "Other".

peterpoe’s picture

FileSize
16.19 KB
6.02 KB
7.75 KB

Now I get it: the setup I suggested in #137 will not work:
Setup:
Setup
Triggered:
Triggered
Not triggered:
Not triggered

Still, seems more a Feature request than a Bug to me, something like a new Edit context setting: "Save dependee values even if the dependency was not triggered".

alanburke’s picture

youkho’s picture

#139 patch fixed this issue

DarkteK’s picture

#139 works excellent, thank you !

Version: 7.x-3.0-alpha2

sergei_brill’s picture

The patch works for me.

czigor’s picture

Status: Needs work » Needs review
FileSize
7.69 KB
1.3 KB

Adding support for 'checked' and '!checked' conditions.

kenorb’s picture

simonbcfa’s picture

#139 is a go for me :)

paulwdru’s picture

sketman’s picture

#139 works for me...

ultimike’s picture

This is _mostly_ working for me, but with patch 143 applied, I'm getting a validation PHP Notice.

Here's how to easily recreate it:

  1. Start with a fresh D7 site.
  2. Install Field Group and Conditional Fields.
  3. Configure the article content type to have a Vertical Tabs Group with a Vertical Tab inside it.
  4. Add a (boolean) single on/off checkbox to the content type, place it in the Vertical Tab.
  5. Add a (List (integer)) select list to the content type, make it required, place it in the vertical tab - see https://www.evernote.com/l/AAM96Y2l7YpMgKPYMrHc26JppMvFXKGMOcE
  6. Create the dependency as seen here: https://www.evernote.com/l/AANNRTJj7xVKuKyQiTQ5OrDYCEQI_nhaF6I
  7. Go to node/add/article, don't populate any part of the form, and hit submit.

Notice: Undefined offset: 0 in conditional_fields_evaluate_dependency() (line 1161 of /Users/michael/Sites/dco_lesson5/sites/all/modules/contrib/conditional_fields/conditional_fields.module).

-mike

seehawk’s picture

#139 worked for me as well. Thanks!

gsoullier’s picture

Hello, is this patch gonna be applied one day ?

sk2013’s picture

Thank you for your patch

#139 worked for me as well.
After applying this patch. required of dependent field is not working.
Example
We have two fields
field1 with checkboxes
field2 with textfield and also required is enabled.
we set visible states with has all the values condition in field dependency

if we select the field1,it shows field2.
But the required is not validated, even field2 has empty or no value.
Any help and support is appreciated.

ultimike’s picture

I just updated @czigor's patch from 142 with some changes (see interdiff) that fix(?) the PHP Notices I was experiencing.

-mike

SaytO’s picture

Hello,

I tried several versions of this patch, but it has worked for me is the number 139.

I wanted a required field if a field has the x values of a taxonomy.

In the latest version of the patch there is an error, I mention it for the next release and possible correction.

This is the error:
Warning: Invalid argument supplied for foreach() en conditional_fields_dependent_validate() (line 679 /conditional_fields/conditional_fields.module).

I hope I have helped.

Thank you very much and best regards.

kespinosa05@gmail.com’s picture

there is patch integrating some patch
conditional_fields-fixed_multiple_field_collection_fields-1464950-276.patch and conditional_fields-conditionally_required-1561272-152.patch into 7.x-3.x branch of yesterday both works for me.

ultimike’s picture

I'm still having an issue with this, and I think it might have something to do with Conditional Fields and Field Group module. I've spent a few hours tracing the code using a debugger, and I don't feel like I've made all that much progress.

My situation is this:

I have a content type with 4 vertical tabs in a single vertical tab group.
Each vertical tab has a checkbox and a float (required) field.
There are 4 conditional fields - each float is set to be visible when the checkbox is unchecked. Therefore, when the checkbox is unchecked, the form should see that the float field is required. When the checkbox is checked, the form should not treat the float as required.

Unfortunately, this behavior does not happen. When the checkboxes are not checked, and the float fields are visible, they are not treated as required (even though they are marked as such).

I have created a simple feature (attached) with the configuration. Note that I'm using the latest -dev version of Conditional Fields with the patch from comment 152 above.

thanks,
-mike

ultimike’s picture

I'm officially offering a cash bounty for this bug to be fixed. See my comment 155 above.

$200 USD to anyone who can fix this.

To qualify for the bounty, the issue in comment 155 must be fixed in a way that satisfies the maintainers of this module.

thanks,
-mike

DamienMcKenna’s picture

Would it help to add some tests for this?

EternalLight’s picture

I've spent in a debugger for a while, and it seems that missing required validation for required fields has nothing to do with the Field Group module. In the field validation callback, conditional_fields_dependent_validate, all field's errors are moved to $form_state['conditional_fields_untriggered_dependents'], which is later used in conditional_fields_form_validate. conditional_fields_form_validateremoves all previous form errors and restores them without the errors that are caused by dependent fields. This behavior was introduced in patch #152.
Unfortunately, required validation on fields that are conditionally required (the initial issue) is not working for me - neither in -dev version nor in -dev version with #152.

EternalLight’s picture

I have made some fixes on top of #152, which cover:
- Brought conditionally required validation for initially not required fields
- Brought conditionally optional validation (skipping required) for initially required fields
- Removed required validation for fields that became conditionally invisible
- Fixed skipping required validation for fields, visibility of which depend on another field's value (#155 case)
Apply on dev version.
Please let me know if it works for you.

ultimike’s picture

@EternalLight - Wow - thank you!

I just tested it and it appears to be working for me. I'd love to hear from others if it works for them as well.

thanks,
-mike

Leeteq’s picture

If we can get one or two more confirmations on the last patch, then given the gravity of this issue (pot. data loss) and the state of this module (issue is from 2012, module in alpha, no commits last 6 months), I'd say push it to -dev to get even more people to try it out and provide feedback.

dtamajon’s picture

Hi! It is working in my environment too.

sergei_brill’s picture

In the function conditional_fields_dependent_validate we have to add an additional condition to check if the $element is accessible, smth like

if (isset($element['#access']) && !$element['#access']) {
  return;
}

in the top of the function.
I wasn't able to submit a complex form where an field is conditionally required, but at the same time can in some situations can be hidden from the form at all using '#access' => false.

EternalLight’s picture

Sure. I can add it to the patch. Or we can wait the maintainer to merge it all into the dev branch.

xaa’s picture

#159 thanks for the patch. Applied with success and working fine for text and list fields. However it seems not working for Boolean fields. Here boolean field is not required even if a conditional rule defined.

edit: the patch is working fine (for boolean type as well).

arunkumark’s picture

Status: Needs review » Reviewed & tested by the community

Hi,

Patch #159 is working as expected.

mvonfrie’s picture

Status: Reviewed & tested by the community » Needs review

Patch #159 applied manually to 7.x-3.x-dev (module installed on 2016-12-15) because automatic apply did not work.

Patch doesn't work.

I have the following structure:
Fields:

  1. field_auction_mode, Type List (Integer), possible values: [1, 2]
  2. field_email_spec_req_subject, Type Text
  3. field_email_spec_req_body, Type Long text

Dependencies:

  1. field_email_spec_req_subject is invisible when field_auction_mode has value "1"
    AND
    field_email_spec_req_subject is required when field_auction_mode has value "2"
  2. field_email_spec_req_body is invisible when field_auction_mode has value "1"
    AND
    field_email_spec_req_body is required when field_auction_mode has value "2"
ursatuba’s picture

Patch from #152 works on the dev version of the module and resolves the required condition in my case.

Used in Drupal Commerce and without field group.

manuelBS’s picture

Confirm that patch#152 semantically works well!

super_romeo’s picture

patch #159 resolves required problem. But...

My case: I have dependent required text field and I validate it via '#element_validate':

function rg_common_field_widget_form_alter(array &$element, array &$form_state, array $context) {
  if ($context['field']['field_name'] == 'field_phone') {
    $element['#element_validate'][] = 'rg_common__phone_validate';
  }
}

function rg_common__phone_validate($element, &$form_state, $form) {
  form_error($element, t('error'));
}

And despite the fact that my field is triggered, this code still runs and hide my field error.

$untriggered_dependents_errors = array_merge($untriggered_dependents_errors, $field['errors']);
aytee’s picture

Patch #159 works for me. Thanks!

jimmynash’s picture

Patch #159 works for me.

joelstein’s picture

Patch from #159 applied cleanly to the latest dev branch and successfully enforces a dependent field collection to be required.

echoz’s picture

For those that have tested it, is patch #159 ready to go back to RTBC?

joelstein’s picture

Status: Needs review » Reviewed & tested by the community

I think so.

dfish17’s picture

Patch #159 worked for me after initially deploying, but after I saved the node more than once it no longer evaluated the field as required.

dfish17’s picture

Patch #159 - My above comment is inaccurate. It wasn't until I changed my dependent field (checkbox referencing a taxonomy term) to be able to have more than one value did the required field check fail.

This scenario works fine (Checkboxes allow only one value). Save fails as expected if Dropdown 1 isn't populated.
Checkbox 1
Checkbox 2
Checkbox 3 - Checked - If checked display dropdown and make it required.
Dropdown 1 = null -> Error "Dropdown 1 is required if Checkbox 3 is selected"

This scenario fails (Checkboxes allow only TWO value) It does not catch Dropdown 1 as required.
Checkbox 1
Checkbox 2 - checked
Checkbox 3 - checked - If checked display dropdown and make it required.
Dropdown 1 - No Error. Saves with null value.

Any ideas on how to patch this scenario?

JugglerX’s picture

Patch #139 worked for me. I am using a term_reference field (widget type: select list) as the controller.

Patch #152 did not work for me.

carrotandme’s picture

Patch #152 worked for me.

drupalevangelist’s picture

#159 worked great! Thank you @EternalLight

SerkanB’s picture

After #159 I still had a issue with multiple fields reacting on a "checkbox-list".

My Case:
dependee as text-list with checkboxes, lets say values 1-10 with multiple-values (otherwise it would be radios...)
10 fields where each of them is only shown when a checkbox is checked.

So, when I check #1, Field #1 is shown, when #2 is checked Field #2 is shown, etc. All of these fields are requiered.

It worked fine, as long as I had only checked a single box. As soon as a second box was checked the fields weren't recognized as "triggered", so the validation was skipped.

The attached patch (seems to) solve the issue I had, but you should be able to apply this over #159, as it does cover more. This one is just an addition to that.

The "view" context wasn't tested that much, I don't even show those field anywere. but I check for the context before doing anything, so in the worst case the view will be as broken as it was before.

bigwebfoundry’s picture

I want to apply #159 on 7.x-3.0-alpha2 and getting following error

Hunk #1 FAILED at 541.
Hunk #2 succeeded at 672 (offset -3 lines).
Hunk #3 succeeded at 729 (offset -2 lines).
Hunk #4 succeeded at 800 (offset -2 lines).
Hunk #5 FAILED at 1042.
Hunk #6 succeeded at 1079 (offset -23 lines).
Hunk #7 succeeded at 1126 (offset -23 lines).

with command :
$ patch < conditional_fields-conditionally_required-1561272-159.patch
I tried with -p0 -p1 but same error , please help

echoz’s picture

@r.singh patches are usually written against dev, as this one is.

bigwebfoundry’s picture

Hi @echoz thank you for response, is there any way to getting this patch apply on module running on live ?

gambry’s picture

@r.singh you'll need the dev version of the module, but be sure you understand what using a -dev version on Production involves.

I don't understand the state of this issue.
#159 has been RTBC however #163, #167, #170 #181 reports issues.

Also #181 suggested additional code, which I'm going to hide from the Files list to avoid confusion.

Leaving to RTBC only because we need maintainer attention and half solution on #159 is better than data lost happening without.

Zekvyrin’s picture

Hello everyone and thanks for the contributions.

Because I needed both the patches I combined 159 & 181 patches and #163 suggestion.

Also, I added an extra piece of code to check if a field is empty using hook_field_is_empty.
This also required me to add another exception for list_boolean fields because of a core issue #2028085: list_field_is_empty() always returns FALSE for list_boolean fields.

I hope it helps...

Zekvyrin’s picture

Fixing typo.. sorry :/

Zekvyrin’s picture

DamienMcKenna’s picture

Status: Reviewed & tested by the community » Needs review

This needs to go back to "needs review" for the new patch(es).

super_romeo’s picture

As I wrote in #170:

My case: I have dependent required text field and I validate it via '#element_validate':

function rg_common_field_widget_form_alter(array &$element, array &$form_state, array $context) {
  if ($context['field']['field_name'] == 'field_phone') {
    $element['#element_validate'][] = 'rg_common__phone_validate';
  }
}

function rg_common__phone_validate($element, &$form_state, $form) {
  form_error($element, t('error'));
}

But my error not fires, because this code hide my field error.

$untriggered_dependents_errors = array_merge($untriggered_dependents_errors, $field['errors']);
senzaesclusiva’s picture

#188 works for me
I have a select list, that with certain values (keys) filled , made states of an other field required, and node can't be saved whitout.
If after i select again a "neutral" value, red asterisk go away, alert message "field_something is required" still remain, but node can be saved.

Pity for messagge if no more required, which can create confusion to users ....

scott.whittaker’s picture

Patch #188 works for me (conditional_fields-7.x-3.x-dev)

chrislabeard’s picture

#188 seems to work for the most part.

If I set a field to required based on a value in a checkbox field. It works but as soon as you select two options from the checkbox. The entire conditional validation is skipped.

SocialNicheGuru’s picture

Just placing this note.

Conflicts with this patch:

https://www.drupal.org/node/1464950#comment-12140055

Donit’s picture

#188 works on conditional_fields-7.x-3.x-dev - at least for single fields.

Unfortunately, I can affirm SocialNicheGuru issue that it doesn't work with field collections and the patches provided in https://www.drupal.org/node/1464950 (tried the latest one).

Is there someone willing to make both patches work together?

NWOM’s picture

Status: Needs review » Needs work

#188 sadly not does work in the circumstance that you have a select (options) field becoming visible and being set to required at the same time. The field comes into display as expected, and the red asterisk is shown. However, the form can be saved without filling out the field.

In the circumstance that the field is only conditionally required, and not conditionally visible as well, an error is instead displayed as expected.

This problem also appears to exist in D8: #2859667: Conditional Required Field not Evaluating on Save, so I'm not sure if it should be included in this patch or if it should be separate.

Setting this to needs work due to this problem (for now) and the one mentioned in #193.

Raz Veinz’s picture

Patch #159 works for me, it will be excellent if required field box turn red if empty / no value. thanks!

asntbladewriter’s picture

#188 does not work for me. Patch fails on Hunk 1 at 541 and on Hunk 5 at 1072. Any assistance would be appreciated

Edit: Was not using the -dev version. Please disregard.

geek-merlin’s picture

Status: Needs work » Postponed (maintainer needs more info)

The latest commits fixed some required-issues. Is this still an issue?

MustangGB’s picture

Status: Postponed (maintainer needs more info) » Needs work

What latest commits?
The most recent commit on the 7.x branch is two and a half years old, so I'm going to say yes, this is still an issue.

kyoder’s picture

The latest patch #188 did not fix the issue for me.

lunk rat’s picture

I still see this issue with the latest dev, or alpha 2, or either patched with #188.

geek-merlin’s picture

Sorry for the confusion. I added some commits in D8 that fix the same problem. I have no idea if this can help with D7 code.

kyoder’s picture

axel.rutz if it's not too much trouble, could you link the relevant 8.x commits you mentioned? They may be of help in making a patch for 7.x

kyoder’s picture

I'm assuming the aforementioned relevant patch for 8.x is http://cgit.drupalcode.org/conditional_fields/commit/?id=836d70aa0850969...

I tested the patch on 7.x and it fixes the frontend validation of a field that has been set to be required through conditional fields (the Form State under Context settings) but it doesn't address the backend validation.

If a field has been configured to be required in its base config (not conditional field's frontend config) and then is set to be visible via conditional fields it is still broken even with the patch. The form can be successfully submitted and circumvents the backend validation of the now-visible required field.

onejam’s picture

I ran into the same issue on 7.x dev version. The 'required' validation does nothing until i patched the module using the patch in comment #188. That fixed it.

cmseasy’s picture

Patch #188 did the jobon 7.x-3.0-alpha2+10-dev, thanks.

p-neyens’s picture

After applying patch #188 i got following warnings.

Invalid argument supplied for foreach() conditional_fields.module:1257.

This when de context is equal to 'view' and the options['condition'] in one off the following (checked, !checked) and not a empty $values array.

NWOM’s picture

FileSize
9.74 KB

Here is a re-roll of #208, since it did not apply against the latest dev (or stable).

earthangelconsulting’s picture

i can confirm that the patch in #209 works! but it must be applied against the latest dev release, 7.x-3.0-alpha2+10-dev

and i beg the module maintainers... PLEASE incorporate this patch into the next 7.x release!

this is a great module, but the lack of proper server-side validation (on conditional-but-required fields) was a big disappointment when i first tried it, and caused some of us to go back to hand-coding special js and php for each case...

and this patch fixes that glaring problem (at least, glaring to some of us ;-) for others it may not apply at all) and does it very well!

thanks NWOM ! it's very useful!

ram4nd’s picture

Status: Needs work » Reviewed & tested by the community

Based on #210.

skylord’s picture

Confirm #209 works OK!

laryn’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
9.68 KB

I just tested and merged this into the latest Backdrop release of Conditional Fields (thanks to all who prepared this patch over the years!)

I found that it only needed one change. As is in #209 it works fine for the 'has value' condition but it was still allowing an empty but conditionally required field to be saved without an error or warning for the 'checked' condition.

All I had to change was to remove this bit from one of the if structures in the patch from #209:

|| in_array($options['condition'], ['checked', '!checked'])

A new patch with this change is attached.

mbnsorg’s picture

Thanks to all for contributions on this issue - this has been super helpful.

Our scenario for this comment here is a conditional field within a field collection, where the dependent field is required but left empty.

With Patch #213 and PHP 7.4 we are getting this:
Notice: Undefined offset: 0 in conditional_fields_evaluate_dependency() (line 1199 of D:\Apache24\htdocs\sites\all\modules\conditional_fields\conditional_fields.module)

We haven't tried debugging the workflow, but just noting we are making this change to line 1197:
if (isset($options['value'][0][$key]) && count($options['value'])) {