Problem

Made fields visible and required when a checkbox field is checked.

Steps to reproduce

Created a field of type checkbox and an image field. The image field is configured to be visible when checkbox is checked. and also, it should be required in that case. But the field slides down and upon checking and unchecking the checkbox. But the required functionality is not working.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

er.garg.karan created an issue. See original summary.

er.garg.karan’s picture

Status: Active » Needs review
StatusFileSize
new0 bytes
er.garg.karan’s picture

Added empty patch file by mistake. This is the one which worked for me.

guicom’s picture

Thank you er.garg.karan, your patch work like a charm for me !!

alfattal’s picture

Patch in #3 worked for me only after applying a core patch from this issue. However, it's throwing this undefined index error Notice: Undefined index: #entity_type in Drupal\conditional_fields\ConditionalFieldsFormHelper::dependentValidate() (line 358 of modules/contrib/conditional_fields/src/ConditionalFieldsFormHelper.php).

sim_1’s picture

Version: 8.x-1.0-alpha9 » 4.x-dev

Updating the target version. This patch works but it needs to be rerolled.

sim_1’s picture

Status: Needs review » Reviewed & tested by the community

Ok after rerolling and testing @er.garg.karan's patch it worked great for me and automated tests passed as well. I didn't see the same warning @alfattal noticed. Marking RTBC.

peoriadrupaluser’s picture

StatusFileSize
new30.29 KB

Hi, unfortunately this patch is not working for me as I would expect it to. I have installed 4.0.0-alpha2 and this patch using composer. I have verified that the code for the patch is in the source for my site.

I am trying to setup a set of conditions where if a checkbox is unchecked the dependent field (a date field) is required. When the checkbox is checked the dependent field should be optional and hidden. The second condition of hiding the date. The first condition to make the field required is not working.

List of fields and conditions described above

I have tested that the required field is not working by trying to save a node without the date field and finding that it is not on the list of required fields that do not have values.

I am not seeing why this is not working for me so any suggestions are greatly appreciated.

Thanks,
Josh

aitala’s picture

The patch did not work for me either -

I have a set of four checkboxes that are supposed to control if a link field (url and text) need to be filled out. The four conditions are all 'OR'd together... With all the boxes unchecked, all the link widgets are visible. When I start checking boxes the results are unpredictable.

What did work was changing the controlling field from checkboxes to a pulldown.

Thx
Eric

sim_1’s picture

Status: Reviewed & tested by the community » Needs work

I'm having the same issue. It was working for me before. Now with alpha2 and drupal 9.5 it is no longer working.

saurabh-2k17’s picture

Hi, I am facing the same issue and came across this thread. I see there is also a dependency by Drupal core

I applied #3 patch from here along with other patch from the Drupal core issue, but this is not working.
Furthermore, I am also on Drupal core 9.5 and alpha2 version of module. I am curious to know what was the last Drupal core version for which this was working?

saurabh-2k17’s picture

I am attaching a working patch here.

saurabh-2k17’s picture

StatusFileSize
new816 bytes

Apologies for the wrong naming

saurabh-2k17’s picture

Priority: Critical » Major
Status: Needs work » Needs review
jmester13’s picture

@saurabh-2j17 I've tried your patch and the core patch and am not seeing a solution, but maybe because my setup is slightly different.

I have a required radio button.
When an option is selected, A required textbox should display.
When the option is not selected, the required textbox is hidden but does not allow the form to be submitted.

Also seeing this same behave for a checkbox which when selected should show a required address field.

Still researching and trying to find a solution but wanted to post here and follow.

Thanks!

jmester13’s picture

Alrighty so update here for anyone else who runs into this issue I hope it helps.

I did not use these patches in the end it comes down to logic.

Do not set required on the field itself. Use the conditional logic to set required!

For example
(checkbox) Yes / No
Do you like Pizza?
If Yes, Show text field
If Yes, Make text field required.

For Address fields, I'm still working on a resolution, looking like some type of custom JS.

shani maurya’s picture

Patch in #3 worked for me only. Thank You!

problue solutions’s picture

On Drupal 10.2 with Claro or Gin themes, when the checkbox is first clicked nothing happens, when it is clicked a second time the dependent field appears, and with each subsequent click the field disappears and appears correctly. It is only the very first click that does not work.

If Slide down effect is selected, on the first click it slides down but then immediately disappears again.

Patch #3 and #14 make no difference.

dqd’s picture

Title: Conditional fields required behavior not working with checkbox » Set field to be required not working with checkbox
Priority: Major » Normal
Status: Needs review » Postponed (maintainer needs more info)
Issue tags: -required
StatusFileSize
new800.27 KB

Thanks for the report and the efforts in here +1

Removed tag. Before adding tags read the issue tag guidelines. Do NOT use tags for adding random keywords or duplicating any other fields.

At the moment maintainers cannot reproduce this issue, tested with default Drupal 10.2 core and Claro setup (watch screen-cast). Please provide more reports and a screen-cast showing setup, themes and modules installed, so that we can make sure that this is really caused by CF.

Note: It makes a difference if the image field was set as required by default or not before, or if the checkbox has been checked already before or not for testing. To be able to turn off the required mode is already an issue because this is not working at the moment for good reasons. Therefor please follow: #3344587: Support for field hidden by condition losing its required status. In this issue you can find a trace back to another issue with a commit which has caused the momentary behavior of CF to not disable required mode.