I have a checkbox that controls the visibility of a required field. That is, when the checkbox isn't set, the select list is required.

When checking the checkbox, the select list is made invisible by Conditional Fields, but upon submit Drupal complains that the hidden field is required.

Is Conditional FIelds able to override the required check when hiding or disabling a field?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

AtomicTangerine’s picture

Hello there, I've been reading through similar past issues in Conditional Fields, seems as though this is the only thread for this issue ("required" property of control fields for a d7 version).

According to this comment: http://drupal.org/node/453204#comment-4439084 from peterpoe (developer for Conditional Fields), it was only just added in May for the d7 dev, but as for the couple tests I've done, I don't see this functionality at all. I have my dependent and dependee both inside the same field group (so that's not the issue), with the select list (currently required) dependent on a certain box being checked. I would like that required property overridden as the dependency condition is made true (checking a box). Right now, the select list is being hidden, but since the field is required, it complains about not completing a required field. Also, my use case is that the box is asking if a seizure occurred, then the required select list is which staff member responded to the incident.

The thread above, that I linked to, describes the long process of getting all this worked out with d7, including in and around field groups.

If there is a way to override this required property, and I'm just not seeing it, anyone feel free to share what you know. There are a lot of config options for dependencies, and I admit I haven't tried every one to see what happens.

I'm working off the July 1st 7.x-3.x-dev version

MrHaroldA’s picture

@AtomicTangerine: it seems the functionality should be there, according to the comment by Peterpoe... Weird...

AtomicTangerine’s picture

Component: Code » User interface

Pull up a chair....
Ok, after playing around with different scenarios between 2 checkbox fields (both defined by allowed values when creating field), I had success with my situation, and sort of with yours too (there's a workaround, if that's the correct term).

So, I was using 2 checkbox fields like I said, but there was some bug with the dependent's label not showing up. I applied patch: http://drupal.org/node/1215826#comment-4721866 which fixed the checkboxes/radios label issue. I know your dependent field was/is a select list, but I at least thought that the label might have been disappearing because of something related to an attempt at overriding the required property. Turns out they were separate (I think...). But I'm glad for that patch because I was pretty disheartened when I saw two bugs :(

Others, this is zher special case and probably doesn't apply to you....see below for @everyone else

BUT for MrHaroldA's SPECIFIC CASE (capped so people don't get confused and run off) I set up a dependency like so:
dependent: field_a dependee: field_b field_a is visible when field_b has value, at least one of the values 1, 2

Stay with me here MrHaroldA, test it for yourself if you need to or can. This "at least" is an option when you select the Value on the Conditions list. 1 and 2 are the keys for the other items on my list(0='Yes', 1='No', 2='idk' is my list). On the rendered behavior section, I have the first 2 boxes checked because the dependency will be active for all values BUT that one box, and when other boxes are checked, you wanted the dependent field rendered (allow 'required' property), and not rendered when we aren't selecting our "any values". Make sense? So basically, you're doing an inverse dependency. This is a workaround until we get AND/OR support which won't be for a while yet.

So, now you have all your values specified, EXCEPT for that box that will by proxy "do" something, in your "any values". I know I'm probably being really confusing, so I'll do another rephrase to solidify what I've done. Assigning the visible and rendered dependency to every box but one, allows for your special case of wanting to do the exact opposite (check a box to UNrender a field)

@ EVERYONE ELSE: back to everyone else who would read this thread haha. Back to my "playing around", with pretty much every combination of enabled, disabled, rendered, unrendered, visible, etc etc dependencies, that I could think of a use case for, I realized that I was using "Value" for the Condition, which allows for the field to be unrendered until the dependency was met. Voila!!! THIS is how you override 'require', by not rendering the field at all.
As far as I know, this works for single on/off boxes controlling required fields, I only tested that a little, but I haven't found anything wrong yet!!!

Extra for those who aren't picking it up: choose "Value", not "Checked", and the "Value" is you either putting in the key(s) or checking a box yourself, so as to say, THIS is my value. :) (and making sure the first box in rendered behavior is checked, to hide a field when the dependency is activated). It's talking about not rendering the field in the form, which is what we want with this overriding 'require' business.

I know this was long but if you actually read it thank you thank you!! And if you already figured these little details out, good for you, I guess I beat you to letting others know about this semi-confusing but awesome assortment of options.

(btw in case you couldn't tell, peterpoe, your module is great and definitely the most organized issue queue I've ever seen, I love it)

Changed to UI feature request because I think that that rendered behavior list should be available for other choices on the Conditions list. Was there a reason it was restricted to "Value" only?

AtomicTangerine’s picture

Forgot a screenshot for MrHaroldA's case. I'm such a person, that knows that sometimes it takes a while to understand things. So I tend to overexplain, basically in the hopes that that one person out there who's like me, gets a lightbulb moment when someone takes the time to explain and show something :)
(sorry for all the long-winded-ness)

MrHaroldA’s picture

@AtomicTangerine: Wow, thanx for the elaborate answer! Although I didn't have success with it ;)

This is my form:

Organisation
[ ] External
[Select list of internal organisations (required)]

When 'External' is checked, the list of organisations (required) should disappear and of course shouldn't cause any errors upon submitting the form.

Organisation
[x] External

The only way to get a checkbox is to use a binary field and then Conditional Fields can't use the 'Value' setting with a set of values like you did...

AtomicTangerine’s picture

Binary field....? Do you mean a single on/off box? I believe you just don't check the box. Remember, for your very strange case, you want an inverse relationship practically. Showing something when another thing has not happened. Also, if your "binary field" does need keys in 'Value', the keys I had for my list will be different than yours, you define the keys for on/off when you make the field, I can't help you if you don't know the keys to your own data. And no, you can use checkboxes with List(text)/List(integer), List (from the Select or Other module), simply by having something like Yes and No as your options, simple form reorganization.
I was trying to help but getting an answer back of "it doesn't work" doesn't help me to help you, so I find it difficult to reply with anything helpful.

Possible information to provide when you ask people questions, would be describing how something didn't work, or your ideas about it. Hopefully you did research before submitting an issue, so links to comments you found helpful, so your potential answerer has more resources for ideas.

Just suggestions.

MrHaroldA’s picture

@AtomicTangerine,

My sincere apologies if my answer came across as incomplete and/or ignorant. English isn't my native tongue so sometimes I have a hard time expressing myself. I thought adding the form should make all things clear so I didn't bother to write a lot of information down in that reply. Hope this one will clarify things. I really do appreciate your help!!!

I can't get Fields to display a checkbox when using anything other than the binary field. All other attempts ended in displaying a radio field instead of a checkbox. The binary checkbox does have keys/values, but Conditional Fields doesn't allow me to specify a value, only if it's checked or not.

Using a checkbox to control the visibility of a required select list in D6 never was a problem, so I guess my case isn't as strange as it seems...

Thanks again!

Harold

askibinski’s picture

I've got exactly the same problem. To recreate it is simple and not at all an edge case:

In a content-type:
A. create an on/off checkbox (binary)
B. create another field which is required.

Now, create a dependency like this:
When A is checked, hide (set invisible) field B.

This will work, but give an error because the field which was hidden is required.

Desired behaviour: unset any requirements for fields which are hidden/disabled by conditions. I believe this was the way it worked in D6.

In other words: the checkbox should also be able to toggle the requirement in this case. (this could be a handy action for other scenario's too)

peterpoe’s picture

Component: User interface » Code
Category: feature » bug
Status: Active » Fixed

This issue is actually two issues:

1) Configuration of checkboxes: "Checked/Unchecked" or "Value"?

#5 and #8: The solution to your problem was actually hidden in AtomicTangerine's extensive comment #3:

choose "Value", not "Checked", and the "Value" is you either putting in the key(s) or checking a box yourself

The "Checked" and "Unchecked" conditions (like all conditions except "Value") are just presentational: all they do is applying the state using basic States API. If you want to have any of the more advanced functionality of Conditional Fields (like conditionally required fields, entity render logic, etc.) you must use the "Value" condition. This makes sense, because if no value is inserted, CF has no means to know whether the dependency is activated or not.

So, if you want a field to SHOW when a checkbox is CHECKED, select "Value" as Condition, "Insert values from widget..." as Value, and check the box underneath "Insert the value that will trigger the dependency."

If you want a field to HIDE when a checkbox is CHECKED, same settings apply except that you have to leave the box from the widget unchecked.

That said, I understand that with checkboxes in particular this can be confusing, since after all there is only one "Value" involved. I will make it clear in the documentation when I'll get to write it and think ways to avoid confusion in the dependency edit page as well.

2) Single on/off checkbox not working

While investigating 1), I found that single on/off checkboxes behaved quite erratically. I have just committed a fix that should make everything work as expected.

AtomicTangerine’s picture

Thanks so much peterpoe! I knew I wasn't crazy :P
Your work is very much appreciated

mrfelton’s picture

Status: Fixed » Active

Perhaps this works ok for checkboxes now, but it doesn't work for select elements.

DEPENDEE FIELD SETTINGS
Type: Optional select with 4 options, so there is a total of 5 options including the no selection option.
Condition = value
values: none of these values
Set of values = '_none'

DEPENDENT FIELD SETTINGS
Type: Required textfield
form state: visible

The javascript works nicely, and when no selection is made in the dependee select, the dependant textfield is hidden. When you make a selection in the dropdown, they become visible.

However when submitting the form with no selection in the dependee (therefore the other textfield if hidden and should be required) the form produces an error stating that the text field is required. This is the same issue I reported in #1227692: Required fields still required even when they are hidden.

mrfelton’s picture

Here is a patch that resolves the issue.

mrfelton’s picture

Sorry, here id the patch again, last one was corrupt somehow.

peterpoe’s picture

Status: Needs review » Fixed

Committed #13 with changes: I left the "_none" value when a Set of values is provided, since otherwise there would be no other way to choose "empty field" as option with Lists. Thanks!

Status: Fixed » Closed (fixed)

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

xcel’s picture

Issue summary: View changes
Status: Closed (fixed) » Active

Hello

Sorry to jump again into this issue.

I still do not get the same behavior, so I was wondering what I am doing wrong.

I have fields in Drupal Commerce Order that I expose in checkout with the help of Field group and Field group panes.

I have:
- one boolean field called 'action', also required (and people have to choose between 'create' (0) and 'renew' (1). Default value is 'create')
- one text field which is required called 'name'

I want to hide and remove the required state of 'name' when users select 'off'
I manage to hide it but I always get the validation error "Name is required" when I click on 'Go to next step' button.

I have tried the following methods:

A - 'name' is INVISILBLE when 'action' has VALUE 1 (value taken from widget)
B - 'name' is VISIBLE when 'action' has VALUE 0 (value taken from widget) AND 'name' is INVISIBLE when 'action' has VALUE 1 (value taken from widget)
C - 'name' is INVISILBLE when 'action' has VALUE 1 AND 'name' is OPTIONAL when 'action' has VALUE 1
==> None are giving the expected result and I always end up with "name is required"

I even tried to reverse the mechanism: remove the required state from the 'name' field settings and do the following:

D - 'name' is VISIBLE when 'action' has value 0 AND 'name' is "REQUIRED" when action HAS VALUE 0

For this method, people can select 'create' for action, leave 'name' empty and go to next step without anuy error . . .

Could somebody help lol. I'm getting desperate.

Thanks in advance

Allan

System Lord’s picture

I'm using dev (just installed) which I assume has #13 patch included? I still cannot submit a form when there is a hidden required filed.

I have a list(text) with 5 values.
1|1
2|2
3|3
4|4
5|5

I have six other fields.
Comments (not required)
Complaint, Name, Email, Phone, Instructions (all required)

1 and 2 = Complaint, Name, Email, Phone, Instructions (all visible when when these values selected).
3,4,5 = Comment (visible when these values are selected).

Still, when I select 3, 4, or 5 I cannot submit because all required fields (which is hidden with these values) requires input.

I've tried a million +one combinations including the inverse.

Please help.

eMuse_be’s picture

Also still having this issue.

emcniece’s picture

FileSize
331.16 KB

Encountering this as well. Could find a way to provide dev access if it would help!

Posted image help_dependencies.jpg for overview.

  • peterpoe committed efe0848 on 8.x-1.x
    #1212752 by mrfelton: Handle required dependents when Lists have special...
SocialNicheGuru’s picture

Status: Active » Needs review

The last submitted patch, 12: 1212752-conditional_fields-required-select.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 13: 1212752-conditional_fields-required-select.patch, failed testing. View results

SocialNicheGuru’s picture

Status: Needs work » Active

Sorry. I am placing this back to the status that I found it in which is "Active".
@SystemLord might want to create another issue.

I am also experiencing this.