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.
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

simar.java created an issue. See original summary.

er.garg.karan’s picture

@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.

er.garg.karan’s picture

Status: Active » Needs review

Updating bug status.

ultrabob’s picture

I'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.

ultrabob’s picture

Title: Required image field not passing validation » File and Image fields are flagged for violating required validation even when filled

Editing the issue title to provide more detail.

lmingarro’s picture

I had the intention of doing the review but could not reproduce the error on D8.9.13 module version 1.0-alpha9

chrisolof’s picture

Title: File and Image fields are flagged for violating required validation even when filled » File, image, and other compound fields are flagged for violating required validation even when filled
Version: 8.x-1.0-alpha9 » 4.x-dev
Priority: Critical » Normal
Status: Needs review » Needs work

I 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:

  1. As er.garg.karan found, the weight sub-element was utilized to determine if field was filled
  2. Weight sub-element was filled in with a value of 0
  3. Module was checking for the presence of a value with PHP's empty() function, which considers a value of 0 as 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 of 0.

Phone number field:

  1. Label sub-element was utilized to determine if field was filled
  2. Label sub-element does not accept input, and as such contained no value even though the field was filled in
  3. Module conclusion was that the field was not filled

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...

thatguy’s picture

I 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.

chrisolof’s picture

Attached 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.

jatingupta40’s picture

Hello,
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.

katekarpenko’s picture

Hi 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.

marieascomedia’s picture

MR !80 from #11 works great for me.

luciano.barros89’s picture

I 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.