Problem
I have an image field that is dependent on a checkbox field. The dependent field is required when the checkbox is checked. But even when the image is uploaded, I am getting an error that the image field is empty.
Steps to reproduce
- Create a checkbox field
- Create an image field
- Make image field required when the checkbox is checked
- Create a new content and check the checkbox
- Browse image in the image field
- Save the content and you will get an error that the image field is required.
Issue fork conditional_fields-3192502
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
Comment #2
er.garg.karan@simar.java
The issue is in the array key that is used for validating the field. It should be using fid rather than the currently used weight atttribute.
Contributing a patch. Hope this works.
Comment #3
er.garg.karanUpdating bug status.
Comment #4
ultrabob commentedI've run into the same problem on a file field. I haven't reviewed the code deeply enough to verify that it is the right solution to the problem, but applying the patch allows me to save successfully where I couldn't before.
Comment #5
ultrabob commentedEditing the issue title to provide more detail.
Comment #6
lmingarro commentedI had the intention of doing the review but could not reproduce the error on D8.9.13 module version 1.0-alpha9
Comment #7
chrisolofI just hit this bug with a multi-image field and a phone number field. Both are compound fields - essentially fields with multiple sub-input elements.
Different issues were at fault for these two fields:
Multi-image field:
00as empty. Module conclusion was that the field was not filled, even though it was. Problematic here, and for other inputs where a user may legitimately specify a value of0.Phone number field:
While er.garg.karan's patch will fix the bug for image and file fields, the bug will still remain for other compound fields (like a phone number field).
It might be worth trying the route require_on_publish took with setting a constraint instead of trying to determine what sub-element is the field's correct sub-element to test for empty in Drupal's vast sea of compound fields (core and contrib). With an entity-level constraint, we could use the isEmpty() method on the fields themselves to see if they're filled or not.
Actually, quite a bit of the server-side functionality of this module could exist via a constraint...
Comment #8
thatguy commentedI had issues with File field conditions and fixed them with this patch but it only applies for file fields. This issue would need more attention.
Comment #9
chrisolofAttached is a patch against the current 4.x branch that brings in support for compound fields of type managed_file and phone_number.
I look at this patch as a temporary fix, but not what we'd want to merge into this module. I still think the constraint route outlined above in #7 is the way to go as it would bring in support for nearly all compound fields and potentially reduce this module's codebase at the same time.
Comment #10
jatingupta40 commentedHello,
Even though I have added the video, it shows the required error.
For v4.0.0-alpha5 this issue is not coming to me but I upgraded it to v4.0.0-alpha6 and got this same error.
Comment #12
katekarpenko commentedHi everyone,
I tested MR !80 from #11 and it works great for image/file fields, but text format widgets (like body field) are still failing validation even when filled.
The issue is that for text format widgets, `field_parents` includes sub-elements like `['field_name', '0', 'format', 'guidelines']` instead of just `['field_name']`, so the validation looks in the wrong place.
I created a patch that applies on top of MR !80 to handle text format widgets the same way it handles managed_file and phone_number - just strips down field_parents to the field name only for these widget types.
Tested with both text format and image fields as conditionally required - both now validate correctly whether empty or filled.
Attaching the patch.
Comment #13
marieascomedia commentedMR !80 from #11 works great for me.
Comment #14
luciano.barros89I tested MR !80 from #11 locally and it fixed the required validation issue for my use case.
Environment:
* Drupal: 11
* Conditional Fields: 4.0.0-alpha6
* Field type: File upload field
* File usage: PDF upload
* Scenario: the PDF field is conditionally required based on another field value
Before applying MR !80, the PDF field could be submitted empty even when the conditional requirement should make it mandatory.
After applying MR !80 from #11, the PDF field is correctly validated as required when the condition is met.
Thanks for the fix.