Problem/Motivation

If you have a required media field that uses the core media library, and you try to submit it without any values, you get a really unclear error message: "This value should not be null". It offers no clues whatsoever concerning which field it's even talking about. This is pretty bad for UX.

Steps to reproduce

Create a media field on a node type, ensuring it uses the media library and requires at least one selection. Go to create a new node of that type, and submit it without selecting any media. You'll get the world's least helpful error message.

Proposed resolution

Implement a custom validation method, so as to produce a better error. This cannot be done using the Form API's #required property, because the media library widget is a composite field type that persists information around in hidden fields that change during user interaction.

Remaining tasks

Commit the merge request.

User interface changes

When submitting an empty, required media library field, users will get a useful error message, instead of a worthless one.

API changes

None.

Data model changes

None.

Release notes snippet

None needed.

CommentFileSizeAuthor
#70 After patch MR !607 message.png293.5 KBRoshaniBhangale
#70 Before patch MR !607 message.png251.09 KBRoshaniBhangale
#48 After-patch2.png25.06 KBmitthukumawat
#48 After-patch1.png14.86 KBmitthukumawat
#48 Before-patch.png15.76 KBmitthukumawat
#47 Media Field.png51.09 KBmanojithape
#46 interdiff_43-46.txt1.19 KBvsujeetkumar
#46 3160238-46.patch7.85 KBvsujeetkumar
#43 3160238-43.patch6.18 KBKapilV
#42 3160238-42.patch6.19 KBKapilV
#41 Screenshot 2021-04-21 at 1.46.26 PM.png194.53 KBvakulrai
#41 interdiff-3160238-36-41.txt2.32 KBvakulrai
#41 media_widget_error-3160238-41.patch6.21 KBvakulrai
#36 interdiff_31-36.txt1.28 KBravi.shankar
#36 3160238-36_drupal-8_9.patch4.56 KBravi.shankar
#36 3160238-36_drupal-9_2.patch4.5 KBravi.shankar
#32 3160238-after_patch-30.png33.49 KBAbhijith S
#32 3160238-after_patch-25.png32.05 KBAbhijith S
#31 3160238-30.patch4.56 KBgmercer
#28 After applying the patch #26.png80.53 KBjanmejaig
#26 3160238-25.patch4.51 KBsnehalgaikwad
#25 3160238-25-fail.patch1.94 KBsnehalgaikwad
#23 3160238_23.patch4.42 KBvsujeetkumar
#18 Screenshot from 2020-08-31 16-22-05.png105.98 KBthalles
#18 Screenshot from 2020-08-31 16-19-17.png90.63 KBthalles
#14 Screen Shot 2020-08-18 at 11.03.31 AM.png272.8 KBtanubansal
#10 media-library-not-null-3160238-10.patch4.43 KBdishabhadra
#9 media-library-not-null-3160238-9.patch4.43 KBdishabhadra
#6 3160238-6-FAIL.patch1.87 KBphenaproxima
Screenshot 2020-07-20 at 1.04.49 PM.png30.39 KBchandu7929

Issue fork drupal-3160238

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

chandu7929 created an issue. See original summary.

chandu7929’s picture

Version: 9.1.x-dev » 9.0.x-dev
phenaproxima’s picture

Title: This value should not be null » Media Library widget produces "This value should not be null" error when field is required
phenaproxima’s picture

Issue tags: +Needs tests

This will need a test before we can fix it.

phenaproxima’s picture

Assigned: Unassigned » phenaproxima
Issue tags: +Bug Smash Initiative

Self-assigning for test coverage, and adding it to the Bug Smash Initiative since this is clearly a bug.

phenaproxima’s picture

Version: 9.0.x-dev » 9.1.x-dev
Status: Active » Needs review
Issue tags: -Needs tests
FileSize
1.87 KB

Here's a test that is failing for me on localhost.

phenaproxima’s picture

Assigned: phenaproxima » Unassigned

Unassigning from myself, since I don't know how to fix this bug. :)

Status: Needs review » Needs work

The last submitted patch, 6: 3160238-6-FAIL.patch, failed testing. View results

dishabhadra’s picture

Fixed the issue related to error message when media field is required.

Instead of "This value should not be null" now error message will be "@field_name field is required."

I tried these patch https://www.drupal.org/files/issues/2020-08-10/3160238-6-FAIL.patch in my local and now it is passing all test cases. So adding test cases in my patch.

Test case output:

Testing Drupal\Tests\media_library\Functional\FieldWidgetTest
Test 'Drupal\Tests\media_library\Functional\FieldWidgetTest::testEmptyValue' started
Test 'Drupal\Tests\media_library\Functional\FieldWidgetTest::testEmptyValue' ended


OK (1 test, 9 assertions)
dishabhadra’s picture

I tried to apply the patch of comment #9 using the composer on 9.0.x core version but the patch was failing.
So I refactored the patch for 9.0.x as well.

mikemadison’s picture

Confirmed that the patch applies and resolves the issue for me! Thank you!

mikemadison’s picture

Status: Needs review » Reviewed & tested by the community
phenaproxima’s picture

Status: Reviewed & tested by the community » Needs review

I'm not 100% certain about the proposed solution here.

I'd like to drill into it a bit further to understand why it's producing this error. The media library is a compound field widget, and I'd like to know if this is something that's been encountered by other compound widgets, either in core or contrib, as well.

tanubansal’s picture

Tested via #10 and above mentioned test steps. It working fine for me after adding the patch
This can be moved to RTBC

anu.a_95’s picture

Couldn't reproduce the issue

I followed the steps:-

  • Modified the default "Basic Page" content type and added an image field marked as required.
  • Tried creating a new page content by filling Title field and body field and leaving media field blank (Didn't browsed and selected image file)
  • Clicked Save Button
  • Error message shown as " field is required"

It would be helpful, if someone could specify whether a new Page content type is created and content added to produce the issue. Adding screenshots of the issue before applying the patch would be really helpful.

phenaproxima’s picture

phenaproxima’s picture

Issue tags: +Usability
thalles’s picture

#9 works to me:

Before #9

After #9

But, @phenaproxima do you want a more generic solution?

thalles’s picture

Would the image field also be one of those fields?

janmejaig’s picture

After applying this patch #9 on Drupal 9.1.x , it is working fine and good to go for RTBC .

quietone’s picture

Status: Needs review » Needs work

This needs and Issue Summary update.

Please when making screenshots, it is usually a good idea to update the Issue Summary with that information. It will help anyone working on the issue.

The question in #13 needs to be answered. And in #15 there is a report that the problem can not be reproduced. If that is confirmed this may be a won't fix.

vsujeetkumar’s picture

@anu.a_95 and @quietone I also followed the steps mentioned in #15 however I am able to reproduce the issue.

vsujeetkumar’s picture

Status: Needs work » Needs review
FileSize
4.42 KB

Not able to apply patch #10, So re-roll patch given.

quietone’s picture

Issue summary: View changes
Status: Needs review » Needs work
+++ b/core/modules/media_library/tests/src/Functional/FieldWidgetTest.php
@@ -0,0 +1,65 @@
+    $this->assertSession()->pageTextNotContains('This value should not be null');

This needs to also test that the desired test is displayed, I think it will be "field_media field is required."

And lets have a fail patch as well.

snehalgaikwad’s picture

Status: Needs work » Needs review
FileSize
1.94 KB

Updated test to check new message for required field. Adding tests only patch.

snehalgaikwad’s picture

The last submitted patch, 25: 3160238-25-fail.patch, failed testing. View results

janmejaig’s picture

I have verified the above issue and found that it is working fine after applying the patch #26 on drupal 9.1.x and seems to be good for RTBC. Please find the attachment for the same.

Steps to test :

  1. Add media field in page content type, and mark this as required.
  2. Try creating page content, fill title & body and leave media as blank.
  3. Click Save button.
  4. It will give you message "This value should not be null"

Expected Result:

  1. It should give proper message like "Media field is required."

Actual Result:

  1. It gives message "This value should not be null".

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

tanubansal’s picture

#26 works for me as well.
This can be moved to RTBC + 1

gmercer’s picture

I've made a new version of this patch for core 8.9.1.

Abhijith S’s picture

Applied both patch #25 and #30 . Patch #25 works for 9.2.x and #30 works for 8.9.x respectively. The validation message will be displaying correctly with corresponding field names after applying these patches.

Attaching screenshots below
In Drupal 9.2.x:
d9

In Drupal 8.9.x
d8

Abhijith S’s picture

Status: Needs review » Reviewed & tested by the community
ramonma1989’s picture

The patch in #31 worked form me, i tested it using D8.9.6, the message is ok now but the field does not get highlighted in red like the others.

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

Sorry, found a nit.

+++ b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
@@ -939,4 +942,34 @@ protected static function setFieldState(array $element, FormStateInterface $form
+    // Trigger error if the field is required and no media is present.
+    // Although the Form API's default validation would also catch this, the
+    // validation error message is too vague, so a more precise one is
+    // provided here.

The wrapping here isn't right, needs to be wrapped at 80 columns.

And, the issue summary needs an update. The proposed resolution needs to be completed, and the remaining tasks are out of date. And since this needs screenshots, the latest ones should be in the IS as well.

Thanks.

ravi.shankar’s picture

Here I have addressed line wrapping for both Drupal-9.2.x and Drupal-8.9.x.

atul4drupal’s picture

Status: Needs work » Needs review
ridhimaabrol24’s picture

Hi

I was using this patch for my project. After applying patch #36, the error message has been fixed but the field still doesnt gets highlighted. Will the field highlighting be fixed in this issue?

Thanks!

wwedding’s picture

Status: Needs review » Needs work

Confirming that the required indicator, "*" is still missing from the form in 8.9 but that the error message is as expected.

When I applied the 8.9 patch in #36, it also created an extraneous directory with an identical test in core/b/core/modules/media_library/tests/src/Functional/FieldWidgetTest.php.

I believe we could use a test for the required indicator, since it seems potentially fragile.

I didn't run the changes through code sniffer.

anshul.udapure’s picture

Ref #36 patch - 3160238-36_drupal-8_9.patch , @ravi.shankar , I am trying this patch on drupal version 8.9.x , it is giving
skipped patch error. ( Skipped patch 'core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php'.
).It is not getting apply through composer also. please help on this

vakulrai’s picture

Adding a patch that will take care of highlighting the target error media widget field. Added a small test for the same.
This patch is implemented on 9.2.x.

Thanks

KapilV’s picture

KapilV’s picture

Status: Needs review » Needs work

The last submitted patch, 43: 3160238-43.patch, failed testing. View results

phenaproxima’s picture

Thanks for the patch, @KapilV! In the future, could you also leave a short comment explaining what you changed? That will help reviewers stay oriented and on top of the most recent changes. Thank you :)

vsujeetkumar’s picture

Status: Needs work » Needs review
FileSize
7.85 KB
1.19 KB

@KapilV Please add interdiff also, It will help to understand the changes you have done. Thanks

Fixed the failed tests. Please have a look.

manojithape’s picture

FileSize
51.09 KB

On my local machine, I installed Drupal version 9.2.0-dev and try to reproduce the above issue.
For me, without applying the patch I am able to see

  1. "Media Fields field is required." error message and
  2. Required field highlight in red color.

Note: Image field name is 'Media Fields'. For reference please check the attached screenshot.

mitthukumawat’s picture

FileSize
15.76 KB
14.86 KB
25.06 KB

I am able to reproduce this issue in fresh installed Drupal version 9.0.2-dev. I have successfully applied the patch #46 and it is working as expected.
Adding screenshots for reference.

mitthukumawat’s picture

Status: Needs review » Reviewed & tested by the community
phenaproxima’s picture

Status: Reviewed & tested by the community » Needs review

I think I'd like to give this a thorough review before RTBC; I'm not 100% certain about the approach, and I'm also unclear as to why we're adding libraries.

phenaproxima’s picture

phenaproxima’s picture

I'm changing this issue to a merge request, since it's a lot easier to review changes on Gitlab than in a patch. Let's use that instead. If you aren't familiar with the merge request workflow, there is documentation available at https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa....

Let's stop using patches from here on out. :)

phenaproxima’s picture

Status: Needs review » Needs work

Did a quick review. I think that the main complaint I have is that we shouldn't be doing anything related to CSS, libraries, or presentation in this merge request. All of that stuff is outside the scope of the form system, which I suspect is where the problem is originating. So let's revert all of those changes, and then we'll have a tighter, more focused set of changes to iterate on. :)

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

JeroenT made their first commit to this issue’s fork.

JeroenT’s picture

Status: Needs work » Needs review

I removed the CSS changes from the MR as mentioned in #54.

This reverts the functionality that a required media library field is higlighted with a red border.

phenaproxima’s picture

EDIT: Never mind, had an idea but it turns out it won't work.

phenaproxima’s picture

I don't think this needs the explicit subsystem maintainer review tag anymore.

phenaproxima’s picture

Status: Needs review » Needs work

A couple of outstanding requests for the MR. :)

JeroenT’s picture

Status: Needs work » Needs review
phenaproxima’s picture

Status: Needs review » Needs work

Thanks, @JeroenT! This is definitely looking pretty good. I made a few more suggestions, plus one very easy change :)

JeroenT’s picture

Status: Needs work » Needs review
phenaproxima’s picture

Status: Needs review » Needs work

Back to you! I think this is getting really close. I think I know why tests are failing, too, so kicking back for that to get fixed.

JeroenT’s picture

Only thing left is to move the test to a separate test class. Will try to do this on monday, but if someone can fix this before, feel free to do so.

JeroenT’s picture

Status: Needs work » Needs review

I reverted the test change and moved it back to a separate functional test.

JeroenT’s picture

MR no longer applied, so I merged 9.2.x back into the MR.

JeroenT’s picture

I noticed that saving a required media field, even when the field contained a value, stopped working.

This was because $field_state['items_count'] returned the number of widgets shown, which on creating a node was 0 and when updating an existing field 1. I updated the code to count($field_state['items']).

I also moved the test back to a JS test and added the check if a node can be saved after adding a media item.

RoshaniBhangale’s picture

Assigned: Unassigned » RoshaniBhangale
RoshaniBhangale’s picture

Verified and tested patch MR !607 mergeable (i.e. https://git.drupalcode.org/project/drupal/-/merge_requests/607.patch) on the drupal 9.3.x-dev version. Patch applied successfully.

Testing Steps:

  1. Add media field in page content type, and mark this as required.
  1. Try creating page content, fill title & body and leave media as blank.
  1. Click Save button.
  1. It will give you message "This value should not be null"

Expected Result:

  1. It should give proper message like "Media field is required."
  1. It should also highlight the field which has error.

Actual Results:

  1. It gives message "Media field is required".

Observation:

  1. When the error message is displayed, the field is not getting highlighted in red.

Please refer attached screenshot.
Moving this ticket to needs works.

RoshaniBhangale’s picture

Issue summary: View changes
RoshaniBhangale’s picture

Status: Needs review » Needs work
RoshaniBhangale’s picture

Assigned: RoshaniBhangale » Unassigned
phenaproxima’s picture

Status: Needs work » Needs review

It should also highlight the field which has an error.

An earlier version of the patch did originally do this, but I asked for those changes to be reverted because they were relatively complicated and involved the front-end, which would make it much harder to get this patch fully reviewed and committed. As far as I know, core doesn't really have a way to mark an entire fieldset as having an error, and it is not in scope of this issue to fix that. Maybe open a separate issue to address that generically?

JeroenT’s picture

Yes, I was thinking the same. I created #3222368: Make it possible to highlight a fieldset when containing an error. We can decide there if choose a generic approach or fix this specifically for this use case.

phenaproxima’s picture

Status: Needs review » Needs work

Welp, I'm not seeing anything objectionable here. Posted two nitpicks, but by no means should those block commit.

The only reason I'm kicking this back is because this issue has no summary, and we kinda need one in order for this to land. :)

phenaproxima’s picture

Issue summary: View changes
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs issue summary update

Okay, fixed the IS. Since the nitpicks I found shouldn't block anything, I'm kicking this to RTBC. I've also given it a quick manual test and confirmed that it works as intended.

  • catch committed b6efcaf on 9.3.x
    Issue #3160238 by JeroenT, phenaproxima, vsujeetkumar, ravi.shankar,...

  • catch committed d890c95 on 9.2.x
    Issue #3160238 by JeroenT, phenaproxima, vsujeetkumar, ravi.shankar,...
catch’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 9.3.x, thanks! Also think the new method on the widget class is innocuous enough to backport this to 9.2.x too, so have cherry-picked there.

Status: Fixed » Closed (fixed)

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

GuillaumeDuveau’s picture

For me, those commits seem to introduce a new problem.

I have a node type with a paragraph field. This paragraph has 2 image fields and 1 text field. The 2 image fields were not required and use the Media Library Widget.

With field_permissions, I've set this field to be editable only by certain roles while the others can view the values:
- Create own value for field field_before_after: editor
- Edit anyone's value for field field_before_after : editor
- View anyone's value for field field_before_after: anyone (anonymous + authenticated)

Up to now I had no problems. Since 9.2.3 I'm now having 2 errors saying that my 2 image fields are required, when editing a node that has a referenced paragraph already typed in by an editor.

Not sure but highly probable those commits do more than they should...

Belialius’s picture

Can confirm that I also have issue mentioned in the #82 comment.

dwebpoint’s picture

Priority: Normal » Major

It looks like the fix caused the new issue related to Content translation.
STR
1. Add Media field to any Content Type as required.
2. Add another language /admin/config/regional/language
3. Create content of this CT
4. Uncheck the field from translation settings /admin/config/regional/content-language and select "Hide non translatable fields on translation forms" for the CT
5. Add another translation to #3 node
6. Get error "Field_name is required."

The new issue has been created https://www.drupal.org/project/drupal/issues/3226791

gbotis’s picture

I can confirm that the patch also applies on 10.0.8 and resolves the issue. Thank you.