Needs work
Project:
Conditional Fields
Version:
4.x-dev
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
12 Dec 2024 at 17:08 UTC
Updated:
8 May 2026 at 14:51 UTC
Jump to comment: Most recent, Most recent file

Comments
Comment #2
goose2000 commentedComment #3
bram.velthoven commentedHas there been any progress on this issue? I’m encountering a similar situation where a field becomes visible and required based on a specific field selection, yet the 'N/A' option remains available. Any insights on resolving this?
Comment #5
bram.velthoven commentedI worked on a patch removing the "N/A" option from the field when conditional_fields set the form-items to required. Since this is CSR we needed to do some magic to detect the "N/A" option and hide it accordingly.
I also updated the phpstan because the CI pipeline gave me problems.
Comment #6
ambuj_gupta commentedComment #7
ambuj_gupta commentedComment #8
benstallings commentedClaude Code says:
Potential issues:
1. Hide/show is only inside the legends.length !== 0 branch (lines 141 and 157). If a boolean field renders without a legend element, the N/A option will never be hidden/shown. This seems like it could be a bug — the hide/show should likely happen regardless of whether legends exist, i.e. moved outside the if (legends.length !== 0) block.
2. Selector fragility — [id*="component-none"] is a substring match. If any unrelated input happens to contain "component-none" in its ID, it would be caught. A more precise selector (e.g., [id$="-component-none"] with a suffix match) would be safer, though in practice this is likely fine for Drupal's ID conventions.
3. No value reset — When hiding the N/A option, if it was currently selected, the user is left with a hidden selected option. It might be worth unchecking it and selecting the first visible option when hiding.
PHPStan Baseline
The baseline was expanded significantly with new suppressions. This is fine as a separate concern (updating to a newer PHPStan version or stricter rules), but it's unrelated to the boolean field fix. Mixing unrelated changes in the same branch can make review harder.
Summary
The fix is straightforward and addresses the issue. The main concern is point 1 — the hide/show logic is nested inside if (legends.length !== 0), which means it won't apply to boolean fields that don't render with a . Worth verifying that boolean fields always have a legend in Drupal's form rendering, or moving the hide/show outside that conditional.
Comment #9
jcandan commentedRemoving the note about the duplicate required asterisk since it is addressed in #3476905: Fix required field indicator appears twice.
Comment #10
jcandan commentedComment #11
jcandan commentedComment #12
jcandan commentedComment #14
jcandan commentedComment #15
jcandan commentedSeems this issues has some history in core.
Comment #17
jcandan commentedI am submitting this MR as a draft because it addresses the front-end/UI side of the issue, but there appears to be a deeper back-end validation problem for radio widgets: the empty conditionally-required radio was not failing validation.
If I understand correctly, conditional fields doesn't fiddle with validation handling, it simply removes empty, un-required form values so they're ignored by validation. When conditionally-required, it allows the empty value through, and normal validation kicks back the required error. If that is correct, then Drupal isn't recognizing the conditionally-required radio value as required, even when empty. Not sure if that may still be the case if forced to submit
_none.I tried a workaround (see #1561272: Conditionally required fields are not required) of marking the field both required and conditionally required. That did not work correctly either: validation still fired even when the dependency was not triggered. But, being marked required in field settings, it removes the N/A option as well. Since marking the field required removes the N/A option, this may not be a matter of Conditional Fields failing to neutralize an empty submitted radio value. Instead, it may be that Conditional Fields is not correctly bypassing required validation for radio widgets when the dependency is not triggered.
So, this draft MR improves the UI behavior, but the back-end handling of conditionally required radio widgets needs additional investigation.
Comment #18
jcandan commentedYep, backtracking to current 4.x without the N/A removal, it does require the radio value when "N/A" (
_none) is chosen, but not when empty.The only problem with that is, for Boolean fields, the validation error message is of the first radio option rather than the actual field label. Perhaps there's a core bug reported for this?
Comment #19
jcandan commentedOkay, I think I've boiled it down.
Comment #23
jcandan commentedFor those looking to see the N/A option is not shown when a radio field is required: #2951317: Radios element missing "required" attribute.
Comment #24
jcandan commentedNeeds review. Should try with and without the patch from #2951317: Radios element missing "required" attribute.
Comment #25
benstallings commentedComment #26
jcandan commentedI think we should explicitly test one additional case here: for Boolean radios, does selecting No (
0) pass validation correctly once the field is conditionally required?In current Require on Publish work, Boolean radios pass when Yes is selected, but No is still being treated as empty/required on publish. That suggests we may have a remaining falsy-value handling problem separate from the
N/Ahide/show behavior.That will help confirm whether the current fix fully resolves required-state handling, or whether Boolean
0still needs special handling.Comment #27
jcandan commentedGood news. It seems that is just a problem with Require on Publish. This MR correctly treats No (
0) as a valid response to Boolean Radio fields.So, still just needs verification noted in #24. :)
Comment #28
benstallings commentedI had to roll back the regression test because I couldn't get it working.
Comment #29
jcandan commentedTests would be a good thing to have, yeah.
Comment #30
jcandan commented@benstallings, as noted in #24, were you able to vet this with and without the patch from #2951317: Radios element missing "required" attribute?
Comment #31
jcandan commentedComment #32
benstallings commentedNo, sorry, I did not try it with the patch from the other issue.
Comment #33
jcandan commentedMy local tests were with the patch. So, if you're seeing it worked, then we're verified good-to-go.
Still just needs tests. :)
Comment #34
goose2000 commentedWow, thank you @jcandan - trying on my DEV site.
Comment #35
benstallings commented@jcandan while a test would be nice to have, I don't see a way forward for writing one. Do you? If not, maybe we can set this back to reviewed & tested?