I have some required fields that are only made visible when a selection is made from another unrequired select box. There was some quirky behavior in regards to the required fields, namely that when no selection was made on the top level check box, fields were still being validated as being required.

I find the code in this module incredibly hard to follow and it screws with your mind trying to debug it!

Attached patch resolves the problems that I was having, though I honestly don't know if this is the right way to go about it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mrfelton’s picture

Status: Active » Needs review
FileSize
1.14 KB
peterpoe’s picture

Status: Needs review » Fixed

Today's commit: Remove entity view handlers in favor of a more generic solution should have solved this problem as well. Please test and feel free to reopen if this is not the case.

askibinski’s picture

Status: Fixed » Needs work

Tested with latest dev: it does work a little better but not quite there:

- if a field A was required and hidden by an other field B, the field isn't required anymore (correct behaviour)
- but if the action is undone by setting field B back to the original value, field A is not set back to required (although the red * does re-appear)

askibinski’s picture

I believe this is solved when #262453: Grouping dependencies by dependent with OR/XOR evaluation is solved because then you can add one row to toggle hide/show and another to toggle required?

rooby’s picture

There is still a related issue to this in regards to radio buttons.

When you make a required radio button not required, it passess the required validation.
However, a non-required radio button should have a N/A value, which it doesn't because it has just had its required flag removed and nothing else (See also #1299818: When making radio buttons required using a condition the N/A option is still available).
Because of this you then get an "An illegal choice has been detected. Please contact the site administrator." error, because of #811542: Regression: Required radios throw illegal choice error when none selected.

rooby’s picture

The problem described in #5 only exists if you make the radio required but do not set a default value.
You can work around it by giving it a default value (not ideal in some cases).

HJulien’s picture

I had this problem too and got around it using rules->form events. Here are the steps I used:

Form events has 4 steps and it's explained in the Advanced Help.

1. On rules ->form events, select Enable event activation messages on forms
2. On the form you want to enable the form rules for, click enable first and then a box appears and select activate
3. On rules ->form events, select Display form element IDs
4. Go back to your form and the element IDs you need to create the rule are there.

Then add a rule as usual. The process also added options for what you can do with that particular form.

After you've done all this you can uncheck those two boxes on rules-> form events page.

For the triggered rule itself, this is what I did:

A. On event: select the form you want and select the action. I selected 'form is being validated'.
B. Condition 1: form element has value: the control field details. For example, I have a radio field with yes or no: field_a[value] yes
C. Condition 2: form element has value: the field that is required field_b. I validated it to ensure it was not empty with this snippet and selected the box Negate.
return array( 0 => array('value' => NULL));
D. Do: Set a form error.

This will check that the required field is not left empty. Which is the same thing as making it 'Required' in my instance. I left the conditional field stuff intact so that the second field can appear. You will need to have the second field in the content-type not set to required.

I hope this helps someone!

LoyC’s picture

There are still some field types that is required, but not visible throws error. This is such and old issue, why is this so hard to fix?

iTiZZiMO’s picture

Having the same issue. I am hiding some textfields with a checkbox. My condition is: if checked => !required (optional).
Drupal is giving me an error, that the hidden fields are still required after submitting...

Would really appreciate this being fixed. =)

LTech’s picture

I have a hidden field that is required. Is there a patch that would make it only required if it is visible?

peterpoe’s picture

Status: Needs work » Fixed

#5: This is a Drupal core problem, and will not be addressed by Conditional Fields
#8: Which field types?
#9: Use the condition "value", not "checked" or "unchecked", since whit the latter the condition is not actually evaluated when the form is submitted (this is by design to avoid duplication of functionality. But "checked" or "unchecked" are causing a lot of confusion in users and will be probably removed).
#10: More context please?

#8 and #10, if you have problems with required fields, please open a new issue and specify the exact steps to reproduce them, thanks.

Nikdilis’s picture

Status: Needs work » Fixed
FileSize
47.06 KB
4.02 KB
2.37 KB
4.18 KB

@PeterPoe:
Regarding to #8 and #9 (I think) here is an example:
The fields field_kontoinhaber, field_kontonummer, field_kreditinstitut, field_bankleitzahl are still required even though the option "Paypal" has been selected => See errors.
and vice versa...
Maybe my settings are not correct - if so, please advice.
See attached images. (In the settings.png 'erforderlich' means required and 'sichtbar' means visible)

Nikdilis’s picture

Status: Fixed » Needs work
warmth’s picture

Status: Fixed » Needs work

For me the hidden required fields work fine when I use them in the registry but then there is a problem when the user try to edit its profile. The hidden required fields are mandatory so the user have to unveil them, fill them with something to be able to save the profile. What can I do? Is there anything I can change in the settings to avoid this issue?

aLearningGuy’s picture

subscribe - any idea when this will be working?

rooby’s picture

Priority: Normal » Major

I think this is major because if I submit a form where a hidden field fails validation there is no indication to the user that there has been a validation issue and the form still submits, however some other strange things can happen, for example if you are using the field collection module the field collections will silently fail to insert their values to the database.

I know that is partly a field collection issue (see #1549364: Data not being saved on initial creation), however it is also a problem with silent field validation errors that should not be happening.

Also, the only real work around is to create a non-required field and set it to conditionally be set to required at the same time it is set to visible, but that is not viable due to #1372284: Required field indicated but not enforced, so there isn't really a workaround for this issue currently.

spacer’s picture

I am really struggling with this module - I've read the warning about not using it on production sites, but I don't want to downgrade & rewrite new site to D6 to get dependent fields to work.

The module seems to work fine on my dev site, but the same behaviour cannot be replicated on production site. I've used bundles to import field settings, ctools export to import field validation rules. This all works fine, but I just cannot get past point of hidden fields preventing the form being submitted (hidden fields should only be required if they are visible). I have tried re-creating them many times, tried to get them in the same order (which is a bit random), and reset caches etc. The module code is too complicated for me to even begin to start looking at.

Can anyone offer any help or advice?

UPDATE - it seems I was a bit too hasty in blaming this module. I'm also using Rules Forms Support to provide a custom rule - deactivating the form, and then re-activating it seems to have cured the problem.

scottsawyer’s picture

I am posting here because I see a lot of the other related issues were closed as duplicate. My issue boils down to required dependee throwing validation error when hidden due to unchecked dependent field.

FYI, I am on alpha-1.

I have Rules Form Support installed, but it is not configured on the content type in question.
I have Field Permissions disabled for the required field.
The dependee field is a simple text box.
The dependent field is a checkbox.

The only error is the validation error that the hidden text field dependee is empty.

Does anyone have some advice as to the correct way to configure the dependee to be required and not throw validation error when hidden? What additional information would be helpful?

hkovacs’s picture

Issue summary: View changes
Status: Needs work » Closed (works as designed)

I am using 7.x-3.x-dev and have successfully configured the required field as follows:

In my case, I am using the address widget dependent on a selected value dependee.

Goto 'manage fields' tab
1. Create dependent field (ie address), but don't mark 'required'

Goto 'manage dependencys' tab
2. Choose Dependent field (ie address) and Dependee field (ie my_select_list)
WHERE

Dependent field is 'visible' when Dependee field has value "my value".

Now make another dependency for the same dependent field
3. Choose same as above Dependent field (ie address) and same as above Dependee field (ie my_select_list)
WHERE

Dependent field is 'required' when Dependee field has value "my value".

This effectively allows the field to be 'not required' while hidden, and allows the form to pass submission without mysterious failure.

IMHO, though this functionality might not have existed in 2011, this functionality works now, so I hope all agree it works now 'as designed'.

All the best.

iqxmk’s picture

I have exactly the same problem.

It's true that for your configuration there is no error message when submitting the form with the hidden "required" field.

But unluckily the field isn't required too when it's visible!

The "set required" action in conditional field simply doesnt't seem to work. Does anyone have an idea how to have required invisible fields that are working correctly?

Regards
Martin

TravisJohnston’s picture

As pointed out in #21, #20 doesn't work. It does show the asterisk that it is required but doesn't validate it.

TravisJohnston’s picture

Status: Closed (works as designed) » Needs review
TravisJohnston’s picture

This does seem to work if you apply the patch in #93 located here https://www.drupal.org/node/1561272

kalistos’s picture

No. It doesn't work for me with 1561272-93 patch.

kenorb’s picture

kenorb’s picture

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

The original issue is 4 years old and I believe the problem was with something else.

I've checked and it works for me with #1561272-93: Conditionally required fields are not required being applied. Without it, it doesn't validate at all.

Please check the following steps #1561272-109: Conditionally required fields are not required. I've tested it today and it seems to work on the latest dev with patch applied.

if you think the problem is still there, please provide reproducible steps in the clean installation.

Btw. Something that could be related as well: #1017882: Required elements buggy with #states

simonbcfa’s picture

#93 linked to from the above post worked for me

kenorb’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)

Since there are no clear reproduction steps and some fix has been committed as per #1 to address the initial problem, and this issue it-self is ambiguous and broad, so different fixes works for different people, so I'm closing this as duplicate as per #27 since people confirmed that for most cases patch from #1561272-93: Conditionally required fields are not required fixes the issue as per #24 & #28.

If anybody has still the problem, it's probably something else (especially when you're saying that you've 'exactly the same problem' without providing any meaningful details), so please follow the linked issues above, otherwise raise a new issue ticket, alternatively provide clear reproducible steps in the summary of this issue.

crutch’s picture

https://www.drupal.org/node/1405702

Required Radio field, when conditionally hidden, produces user feedback error that it is required. This issue seems to have been resolved on the Drupal core side https://www.drupal.org/node/811542.

Current workaround is to make the radio not required, hide the N/A option with css, example #edit-field-industry-sector-und > div:nth-child(1), then create a rule to check condition and produce a specific error message if/when the radio is visible.