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.
Comment | File | Size | Author |
---|---|---|---|
#70 | After patch MR !607 message.png | 293.5 KB | RoshaniBhangale |
#70 | Before patch MR !607 message.png | 251.09 KB | RoshaniBhangale |
#48 | After-patch2.png | 25.06 KB | mitthukumawat |
#48 | After-patch1.png | 14.86 KB | mitthukumawat |
#48 | Before-patch.png | 15.76 KB | mitthukumawat |
Issue fork drupal-3160238
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
chandu7929 CreditAttribution: chandu7929 commentedComment #3
phenaproximaComment #4
phenaproximaThis will need a test before we can fix it.
Comment #5
phenaproximaSelf-assigning for test coverage, and adding it to the Bug Smash Initiative since this is clearly a bug.
Comment #6
phenaproximaHere's a test that is failing for me on localhost.
Comment #7
phenaproximaUnassigning from myself, since I don't know how to fix this bug. :)
Comment #9
dishabhadra CreditAttribution: dishabhadra at Axelerant commentedFixed 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:
Comment #10
dishabhadra CreditAttribution: dishabhadra at Axelerant commentedI 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.
Comment #11
mikemadison CreditAttribution: mikemadison at Acquia commentedConfirmed that the patch applies and resolves the issue for me! Thank you!
Comment #12
mikemadison CreditAttribution: mikemadison at Acquia commentedComment #13
phenaproximaI'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.
Comment #14
tanubansal CreditAttribution: tanubansal at Salsa Digital commentedTested via #10 and above mentioned test steps. It working fine for me after adding the patch
This can be moved to RTBC
Comment #15
anu.a_95 CreditAttribution: anu.a_95 at Zyxware Technologies commentedCouldn't reproduce the issue
I followed the steps:-
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.
Comment #16
phenaproximaComment #17
phenaproximaComment #18
thalles#9 works to me:
Before #9
After #9
But, @phenaproxima do you want a more generic solution?
Comment #19
thallesWould the image field also be one of those fields?
Comment #20
janmejaig CreditAttribution: janmejaig at Srijan | A Material+ Company for Drupal India Association commentedAfter applying this patch #9 on Drupal 9.1.x , it is working fine and good to go for RTBC .
Comment #21
quietone CreditAttribution: quietone as a volunteer commentedThis 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.
Comment #22
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commented@anu.a_95 and @quietone I also followed the steps mentioned in #15 however I am able to reproduce the issue.
Comment #23
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedNot able to apply patch #10, So re-roll patch given.
Comment #24
quietone CreditAttribution: quietone as a volunteer commentedThis 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.
Comment #25
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedUpdated test to check new message for required field. Adding tests only patch.
Comment #26
snehalgaikwad CreditAttribution: snehalgaikwad as a volunteer and at QED42 for Drupal India Association commentedComment #28
janmejaig CreditAttribution: janmejaig at Srijan | A Material+ Company for Drupal India Association commentedI 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 :
Expected Result:
Actual Result:
Comment #30
tanubansal CreditAttribution: tanubansal at Salsa Digital commented#26 works for me as well.
This can be moved to RTBC + 1
Comment #31
gmercer CreditAttribution: gmercer commentedI've made a new version of this patch for core 8.9.1.
Comment #32
Abhijith S CreditAttribution: Abhijith S as a volunteer and at Zyxware Technologies commentedApplied 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:
In Drupal 8.9.x
Comment #33
Abhijith S CreditAttribution: Abhijith S as a volunteer and at Zyxware Technologies commentedComment #34
ramonma1989The 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.
Comment #35
quietone CreditAttribution: quietone as a volunteer commentedSorry, found a nit.
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.
Comment #36
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedHere I have addressed line wrapping for both Drupal-9.2.x and Drupal-8.9.x.
Comment #37
atul4drupal CreditAttribution: atul4drupal at Srijan | A Material+ Company for Drupal India Association commentedComment #38
ridhimaabrol24 CreditAttribution: ridhimaabrol24 at Srijan | A Material+ Company for Drupal India Association commentedHi
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!
Comment #39
wwedding CreditAttribution: wwedding at Sticky Co commentedConfirming 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.
Comment #40
anshul.udapure CreditAttribution: anshul.udapure commentedRef #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
Comment #41
vakulrai CreditAttribution: vakulrai as a volunteer and at QED42 for QED42 commentedAdding 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
Comment #42
KapilV CreditAttribution: KapilV as a volunteer and at Innoraft for Drupal Care, Drupal Association commentedComment #43
KapilV CreditAttribution: KapilV as a volunteer and at Innoraft for Drupal Care, Drupal Association commentedComment #45
phenaproximaThanks 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 :)
Comment #46
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commented@KapilV Please add interdiff also, It will help to understand the changes you have done. Thanks
Fixed the failed tests. Please have a look.
Comment #47
manojithape CreditAttribution: manojithape at QED42 for Drupal India Association commentedOn 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
Note: Image field name is 'Media Fields'. For reference please check the attached screenshot.
Comment #48
mitthukumawat CreditAttribution: mitthukumawat at Zyxware Technologies for Drupal Association commentedI 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.
Comment #49
mitthukumawat CreditAttribution: mitthukumawat at Zyxware Technologies for Drupal Association commentedComment #50
phenaproximaI 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.
Comment #51
phenaproximaComment #52
phenaproximaI'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. :)
Comment #54
phenaproximaDid 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. :)
Comment #57
JeroenTI 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.
Comment #58
phenaproximaEDIT: Never mind, had an idea but it turns out it won't work.
Comment #59
phenaproximaI don't think this needs the explicit subsystem maintainer review tag anymore.
Comment #60
phenaproximaA couple of outstanding requests for the MR. :)
Comment #61
JeroenTComment #62
phenaproximaThanks, @JeroenT! This is definitely looking pretty good. I made a few more suggestions, plus one very easy change :)
Comment #63
JeroenTComment #64
phenaproximaBack 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.
Comment #65
JeroenTOnly 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.
Comment #66
JeroenTI reverted the test change and moved it back to a separate functional test.
Comment #67
JeroenTMR no longer applied, so I merged 9.2.x back into the MR.
Comment #68
JeroenTI 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 tocount($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.
Comment #69
RoshaniBhangale CreditAttribution: RoshaniBhangale for Drupal India Association commentedComment #70
RoshaniBhangale CreditAttribution: RoshaniBhangale at QED42 for Drupal India Association commentedVerified 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:
Expected Result:
Actual Results:
Observation:
Please refer attached screenshot.
Moving this ticket to needs works.
Comment #71
RoshaniBhangale CreditAttribution: RoshaniBhangale for Drupal India Association commentedComment #72
RoshaniBhangale CreditAttribution: RoshaniBhangale at QED42 for Drupal India Association commentedComment #73
RoshaniBhangale CreditAttribution: RoshaniBhangale at QED42 for Drupal India Association commentedComment #74
phenaproximaAn 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?
Comment #75
JeroenTYes, 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.
Comment #76
phenaproximaWelp, 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. :)
Comment #77
phenaproximaOkay, 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.
Comment #80
catchCommitted/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.
Comment #82
GuillaumeDuveauFor 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...
Comment #83
Belialius CreditAttribution: Belialius commentedCan confirm that I also have issue mentioned in the #82 comment.
Comment #84
dwebpoint CreditAttribution: dwebpoint at EPAM Systems commentedIt 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
Comment #85
gbotis CreditAttribution: gbotis commentedI can confirm that the patch also applies on 10.0.8 and resolves the issue. Thank you.