When a paragraph field is set to required, but no entity is added the error message returned is not useful to the end user.

Steps to reproduce:
1. Create a content type
2. Add a required paragraph field to the content type
3. Create a content item of that content type, without adding a value to the required paragraph field

Expected result:
Error message: "{Field name} field is required."

Actual result:
Error message: "This value should not be null."

CommentFileSizeAuthor
#90 paragraphs-empty_required_fields-2788607-90.patch12.58 KBmbm80
#86 paragraphs-empty_required_fields-2788607-86.patch13.32 KBrecrit
#81 empty_required_fields-2788607-81.patch13.32 KBVladimirMarko
#81 interdiff-2788607-77-81.txt5.87 KBVladimirMarko
#81 empty_required_fields-2788607-81-test-only.patch5.38 KBVladimirMarko
#77 empty_required_fields-2788607-77.patch14.07 KBVladimirMarko
#77 interdiff-2788607-73-77.txt5.64 KBVladimirMarko
#77 empty_required_fields-2788607-77-test-only.patch5.87 KBVladimirMarko
#73 empty_required_fields-2788607-73.patch12.8 KBVladimirMarko
#73 interdiff-2788607-70-73.txt2.6 KBVladimirMarko
#73 empty_required_fields-2788607-73-test-only.patch5.82 KBVladimirMarko
#70 empty_required_fields-2788607-70.patch12.95 KBVladimirMarko
#70 interdiff--2788607-66-70.txt4.03 KBVladimirMarko
#66 empty_required_fields-2788607-66.patch12.5 KBVladimirMarko
#66 interdiff-2788607-60-66.txt3.45 KBVladimirMarko
#66 empty_required_fields-2788607-66-test-only.patch5.96 KBVladimirMarko
#64 interdiff-2461695-164-168.txt26.61 KBjeroent
#64 empty_required_fields-2788607-64.patch11.33 KBjeroent
#60 empty_required_fields-2788607-60.patch10.6 KBVladimirMarko
#60 interdiff-2788607-52-60.txt2.19 KBVladimirMarko
#60 empty_required_fields-2788607-60-test-only.patch4.07 KBVladimirMarko
#52 empty_required_fields-2788607-52.patch9.86 KBVladimirMarko
#52 interdiff-2788607-49-52.patch978 bytesVladimirMarko
#52 empty_required_fields-2788607-52-test-only.patch4.07 KBVladimirMarko
#49 empty_required_fields-2788607-49.patch9.87 KBVladimirMarko
#49 interdiff-2788607-45-49.txt7.6 KBVladimirMarko
#49 empty_required_fields-2788607-49-test-only.patch4.08 KBVladimirMarko
#45 empty_required_fields-2788607-45.patch9.31 KBVladimirMarko
#45 interdiff-2788607-37-45.txt3.83 KBVladimirMarko
#45 empty_required_fields-2788607-45-test-only.patch3.52 KBVladimirMarko
#37 empty_required_fields-2788607-37.patch9.42 KBVladimirMarko
#37 interdiff-2788607-31-37.txt2.97 KBVladimirMarko
#37 empty_required_fields-2788607-37-test-only.patch3.66 KBVladimirMarko
#34 the_background_plugin-2847042-3.patch2.02 KBVladimirMarko
#34 interdiff-2788607-31-34.txt2.97 KBVladimirMarko
#34 the_background_plugin-2847042-3-test-only.patch1.07 KBVladimirMarko
#31 empty_required_error_message-2788607-31.patch9.49 KBModernMantra
#28 empty-required-error-message-2788607-28.patch4.69 KBcyb_tachyon
#26 empty-required-error-message-2788607-26.patch4.69 KBcyb_tachyon
#21 empty-required-error-message-2788607-20.patch55.34 KBarpitr
#17 interdiff-2788607-15-17.txt861 bytespradeep22saini
#17 empty-required-error-message-2788607-17.patch4.24 KBpradeep22saini
#14 interdiff-2788607-11-14.txt1.75 KBModernMantra
#14 empty-required-error-message-2788607-15.patch4 KBModernMantra
#11 Screen Shot 2016-08-25 at 09.10.04.png23.08 KBandy_w
#11 interdiff-2788607-9-11.txt798 bytesandy_w
#11 empty-required-error-message-2788607-11.patch2.25 KBandy_w
#9 interdiff-2788607-7-9.txt1.64 KBandy_w
#9 empty-required-error-message-2788607-9.patch2.25 KBandy_w
#7 empty-required-error-message-2788607-2.patch2.77 KBandy_w
#2 empty-required-error-message-2788607.patch1.97 KBandy_w

Comments

andywhale created an issue. See original summary.

andy_w’s picture

StatusFileSize
new1.97 KB

This patch seems to resolve the issue.

johnchque’s picture

Status: Active » Needs review

Sounds reasonable and nice, let's see what test bot has to say :)

Status: Needs review » Needs work

The last submitted patch, 2: empty-required-error-message-2788607.patch, failed testing.

The last submitted patch, 2: empty-required-error-message-2788607.patch, failed testing.

The last submitted patch, 2: empty-required-error-message-2788607.patch, failed testing.

andy_w’s picture

Refactored out to two separate validation methods to prevent the delta issue

johnchque’s picture

Status: Needs work » Needs review

Let's see test bot again, please set the issue as needs review when upload a new patch. And also an interdiff would be useful.

andy_w’s picture

Refactored this to avoid altering the initial elementValidate (no requirement for this), and to avoid checking for an irrelevant error.

johnchque’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
@@ -1051,6 +1052,20 @@ class InlineParagraphsWidget extends WidgetBase {
+    ¶

wrong indentation here. Besides that looks nice.

can we get some example screenshots?

andy_w’s picture

Status: Needs work » Needs review
StatusFileSize
new2.25 KB
new798 bytes
new23.08 KB

Fixed incorrect indentation & include a screenshot of the relevant error message. This message simply replaces the old standard error message so should appear as that error did, screenshot included.

johnchque’s picture

Status: Needs review » Needs work

I tested manually a bit and seems to work nicely, the only problem I found so far is that when a paragraph with a required field is added and we have paragraphs in the same level with display mode as 'closed', we don't fill the required field of the new paragraph and we open and close the previously created paragraph it doesn't allow the user to close the old paragraph pointing to the required field of the new paragraph.

miro_dietiker’s picture

Issue tags: +Needs tests

And yeah, we need the tests. :-)

ModernMantra’s picture

Status: Needs work » Needs review
StatusFileSize
new4 KB
new1.75 KB

Providing test coverage...

pradeep22saini’s picture

Patch works fine.
There is one more update needed.
When viewing the edit form for Paragraph. It doesn't show it as required. It should show "*" red asterisk for required paragraph field.

pradeep22saini’s picture

Status: Needs review » Needs work
pradeep22saini’s picture

Status: Needs work » Needs review
StatusFileSize
new4.24 KB
new861 bytes

Updating class "form-require" changes for require paragraph fields.

michelle’s picture

re #15, there is a patch for that here: #2820502: Add form-required to required paragraph fields. If that will be included in this patch, then that one can be marked dupe.

drholera’s picture

#17 Looks good for me

Status: Needs review » Needs work

The last submitted patch, 17: empty-required-error-message-2788607-17.patch, failed testing.

arpitr’s picture

Status: Needs work » Needs review
StatusFileSize
new55.34 KB

Does not seem to get the #17 patch applied, I did a manual patch using #17 and here goes the patch as attachment.

Status: Needs review » Needs work

The last submitted patch, 21: empty-required-error-message-2788607-20.patch, failed testing.

johnchque’s picture

Issue tags: -Needs tests

@arpitr it seems your dev branch is way below the current dev. Let's retest #17 and see if it passes now.

arpitr’s picture

I realised after uploading the patch and when I saw the diff, please ignore #21

cyb_tachyon’s picture

#17 works for me against latest dev. (2/3 Community Reviews).

However, some coding standards need to be addressed (will roll a patch if I get time).

  • File mode should not be set (100644 I think is default for this repo) Could be wrong on this one

    old mode 100644 / new mode 100755
  • 17 - Drupal Whitespace Operator Before & After

    + '#max_delta' => $max-1, should be + '#max_delta' => $max - 1,
  • 53 - Either use @inheritdoc or completely document the function.
    + * Validate multiple element items. should be + * {@inheritdoc}
  • 80 - Either use @inheritdoc or completely document the function.
    + * Tests displaying error message on empty paragraph required field. should be + * {@inheritdoc}
  • 112 - Should have a blank line after function.
    + } should be + }\n
cyb_tachyon’s picture

  • Re-rolled against latest dev.
  • Fixed coding style issues against d.o standards.
cyb_tachyon’s picture

Component: Code » User interface
StatusFileSize
new4.69 KB
  • Fixed PHP Error with Empty Required field class setting.
zerolab’s picture

Status: Needs work » Needs review
johnchque’s picture

Status: Needs review » Needs work

We need this fix and test in the experimental widget too.

ModernMantra’s picture

Status: Needs work » Needs review
StatusFileSize
new9.49 KB

Rerolled the patch because of recent commit, as well extended the patch to experimental widget (also added test coverage). Hope it is ok :)

miro_dietiker’s picture

Status: Needs review » Needs work

I like it how this is combining the two cases of the form elements, although only very few lines are won.

  1. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -799,30 +799,33 @@ class InlineParagraphsWidget extends WidgetBase {
    +      '#title' => $title,
    ...
             'title' => [
    ...
               '#value' => $title,
    

    The #title should not be set on this level in the second case, or at least wasn't before.
    Applies to both widgets.

  2. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -799,30 +799,33 @@ class InlineParagraphsWidget extends WidgetBase {
    +      $require_class = $this->fieldDefinition->isRequired() ? 'form-required' : '';
    ...
    +          '#attributes' => ['class' => [$require_class]],
    

    I don't think we should create an empty string item here. Make the variable an array with empty default.

VladimirMarko’s picture

Assigned: Unassigned » VladimirMarko
VladimirMarko’s picture

Fixed that.

Disregard the above. It's a patch for an entirely different issue.

VladimirMarko’s picture

Status: Needs work » Needs review
VladimirMarko’s picture

Status: Needs review » Needs work
VladimirMarko’s picture

Status: Needs work » Needs review
StatusFileSize
new3.66 KB
new2.97 KB
new9.42 KB

This should fix it.

The last submitted patch, 34: the_background_plugin-2847042-3-test-only.patch, failed testing.

The last submitted patch, 34: the_background_plugin-2847042-3-test-only.patch, failed testing.

The last submitted patch, 34: the_background_plugin-2847042-3.patch, failed testing.

The last submitted patch, 34: the_background_plugin-2847042-3.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 37: empty_required_fields-2788607-37.patch, failed testing.

wiktorb’s picture

#37 works fine - tested with both classic and experimental widget

johnchque’s picture

#37 is now working, instead the test-only should fail.

VladimirMarko’s picture

Status: Needs work » Needs review
StatusFileSize
new3.52 KB
new3.83 KB
new9.31 KB

The last submitted patch, 45: empty_required_fields-2788607-45-test-only.patch, failed testing.

The last submitted patch, 45: empty_required_fields-2788607-45-test-only.patch, failed testing.

johnchque’s picture

Status: Needs review » Needs work

As discussed let's move the test to a new class for the UI. :)

VladimirMarko’s picture

Status: Needs work » Needs review
StatusFileSize
new4.08 KB
new7.6 KB
new9.87 KB

Moved the tests to a new class, as discussed.

The last submitted patch, 49: empty_required_fields-2788607-49-test-only.patch, failed testing.

The last submitted patch, 49: empty_required_fields-2788607-49-test-only.patch, failed testing.

VladimirMarko’s picture

Converted to use only short array syntax.

The last submitted patch, 52: empty_required_fields-2788607-52-test-only.patch, failed testing.

The last submitted patch, 52: empty_required_fields-2788607-52-test-only.patch, failed testing.

The last submitted patch, 52: interdiff-2788607-49-52.patch, failed testing.

The last submitted patch, 52: interdiff-2788607-49-52.patch, failed testing.

johnchque’s picture

Status: Needs review » Reviewed & tested by the community

Next time be careful with interdiff name. Besides that looks good. Any complaint for RTBC it?

ssibal’s picture

There is still a case where this meaningless error message appears:

Steps to reproduce:
1. Create a content type
2. Add a required paragraph field to the content type
3. Create a content item of that content type
4. Create a paragraph type item and remove it but don't approve the removal
5. Try to save the content

Expected result:
Error message: "{Field name} field is required."

Actual result:
Error message: "This value should not be null."

VladimirMarko’s picture

Status: Reviewed & tested by the community » Needs work

Good catch, @ssibal! I will fix it.

VladimirMarko’s picture

Status: Needs work » Needs review
StatusFileSize
new4.07 KB
new2.19 KB
new10.6 KB

I fixed that case, but did not extend the tests to cover it yet.

The last submitted patch, 60: empty_required_fields-2788607-60-test-only.patch, failed testing.

The last submitted patch, 60: empty_required_fields-2788607-60-test-only.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 60: empty_required_fields-2788607-60.patch, failed testing.

jeroent’s picture

Fixed failing test.

jeroent’s picture

Status: Needs work » Needs review
VladimirMarko’s picture

Extended the test coverage.

The last submitted patch, 66: empty_required_fields-2788607-66-test-only.patch, failed testing.

The last submitted patch, 66: empty_required_fields-2788607-66-test-only.patch, failed testing.

ginovski’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
@@ -1239,6 +1242,31 @@ class InlineParagraphsWidget extends WidgetBase {
   /**
    * {@inheritdoc}
    */
+  public function multipleElementValidate($element, FormStateInterface $form_state, $form) {

This function should have its own documentation, there is nothing from {@inheritdoc}
Same for the one in ParagraphsWidget

VladimirMarko’s picture

Status: Needs work » Needs review
StatusFileSize
new4.03 KB
new12.95 KB

Added the missing method documentation for multipleElementValidate.

a.milkovsky’s picture

The patch #70 forks for me. +1 for RTBC

toncic’s picture

Status: Needs review » Needs work
  1. +++ b/src/Tests/Classic/ParagraphsUiTest.php
    @@ -0,0 +1,75 @@
    +  /**
    +   * {@inheritdoc}
    +   */
    +  protected function setUp() {
    +    parent::setUp();
    +  }
    
    +++ b/src/Tests/Experimental/ParagraphsExperimentalUiTest.php
    @@ -0,0 +1,75 @@
    +  /**
    +   * {@inheritdoc}
    +   */
    +  protected function setUp() {
    +    parent::setUp();
    +  }
    

    We don't need this.

  2. +++ b/src/Tests/Classic/ParagraphsUiTest.php
    @@ -0,0 +1,75 @@
    +    $this->drupalPostForm('admin/structure/types/manage/paragraphed_content_demo/fields/add-field', [
    +      'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
    +      'label' => $field_title,
    +      'field_name' => 'content',
    +    ], t('Save and continue'));
    
    +++ b/src/Tests/Experimental/ParagraphsExperimentalUiTest.php
    @@ -0,0 +1,75 @@
    +    $this->drupalPostForm('admin/structure/types/manage/paragraphed_content_demo/fields/add-field', [
    +      'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
    +      'label' => $field_title,
    +      'field_name' => 'content',
    +    ], t('Save and continue'));
    

    Hmm, this is OK but maybe we can change this a little bit and do on the way which is more common for us with $edit, the same as you did in next few lines. I think will be more readable.

VladimirMarko’s picture

Status: Needs work » Needs review
StatusFileSize
new5.82 KB
new2.6 KB
new12.8 KB

Implemented the changes.

The last submitted patch, 73: empty_required_fields-2788607-73-test-only.patch, failed testing.

The last submitted patch, 73: empty_required_fields-2788607-73-test-only.patch, failed testing.

toncic’s picture

Status: Needs review » Needs work
  1. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -850,30 +850,33 @@ class InlineParagraphsWidget extends WidgetBase {
    +      $classes = $this->fieldDefinition->isRequired() ? ['form-required'] : [];
    

    I would say "class" instead of "classes".

  2. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -881,7 +884,7 @@ class InlineParagraphsWidget extends WidgetBase {
    -          ]
    

    Unrelated change.

  3. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -1237,6 +1240,39 @@ class InlineParagraphsWidget extends WidgetBase {
    +    $remove_mode_item_count = 0;
    +    if (isset($widget_state['paragraphs'])) {
    +      foreach ($widget_state['paragraphs'] as $paragraph) {
    +        if ($paragraph['mode'] == 'remove') {
    +          $remove_mode_item_count++;
    +        }
    +      }
    +    }
    

    Maybe to create helper method and put these code there. Would be nice if we can send mode as a parameter and make it to works for all modes.

  4. +++ b/src/Tests/Classic/ParagraphsUiTest.php
    @@ -0,0 +1,69 @@
    +    $this->drupalPostForm('admin/structure/types/manage/paragraphed_content_demo/fields/add-field', $edit, t('Save and continue'));
    

    The line is so long. Split it in multi lines, drupalGet('.....') before $edit.

VladimirMarko’s picture

Status: Needs work » Needs review
StatusFileSize
new5.87 KB
new5.64 KB
new14.07 KB

1. As discussed: It's an array that contains classes. (One or none, in this case.) We commonly just use plural for those, in Drupal. As in: $file = current($files);, even if we know that there is just one file in $files.

Implemented the rest.

The last submitted patch, 77: empty_required_fields-2788607-77-test-only.patch, failed testing.

The last submitted patch, 77: empty_required_fields-2788607-77-test-only.patch, failed testing.

toncic’s picture

Status: Needs review » Needs work
  1. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -1237,6 +1240,31 @@ class InlineParagraphsWidget extends WidgetBase {
    +    $non_remove_mode_item_count = $widget_state['real_item_count'] - $remove_mode_item_count ;
    
    +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
    @@ -1290,6 +1293,31 @@ class ParagraphsWidget extends WidgetBase {
    +    $non_remove_mode_item_count = $widget_state['real_item_count'] - $remove_mode_item_count ;
    +
    

    Extra space before ';'.

  2. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -1401,6 +1429,32 @@ class InlineParagraphsWidget extends WidgetBase {
    +   * @param string $mode
    +   *   The mode to look for.
    +   * @return int
    
    +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
    @@ -1469,6 +1497,32 @@ class ParagraphsWidget extends WidgetBase {
    +   * @param string $mode
    +   *   The mode to look for.
    +   * @return int
    +   *   The number of paragraphs is the given mode.
    

    Separate the @param and @return sections by a blank line.

  3. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -1401,6 +1429,32 @@ class InlineParagraphsWidget extends WidgetBase {
    +  protected function getNumberOfParagraphsInMode($widget_state, $mode) {
    
    +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
    @@ -1469,6 +1497,32 @@ class ParagraphsWidget extends WidgetBase {
    +  protected function getNumberOfParagraphsInMode($widget_state, $mode) {
    

    Type hint "array" missing for $widget_state.

  4. +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
    @@ -925,7 +928,7 @@ class ParagraphsWidget extends WidgetBase {
    -          ]
    

    Still unrelated change.

  5. +++ b/src/Tests/Classic/ParagraphsUiTest.php
    @@ -0,0 +1,70 @@
    +    $field_title = 'Content Test';
    +    $edit = [
    +      'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
    +      'label' => $field_title,
    +      'field_name' => 'content',
    +    ];
    +    $this->drupalGet('admin/structure/types/manage/paragraphed_content_demo/fields/add-field');
    +    $this->drupalPostForm(NULL, $edit, t('Save and continue'));
    +    $this->drupalPostForm(NULL, [], t('Save field settings'));
    +    $edit = [
    +      'required' => TRUE,
    +    ];
    +    $this->drupalPostForm(NULL, $edit, 'Save settings');
    +    $this->assertText('Saved ' . $field_title . ' configuration.');
    
    +++ b/src/Tests/Experimental/ParagraphsExperimentalUiTest.php
    @@ -0,0 +1,70 @@
    +    $edit = [
    +      'new_storage_type' => 'field_ui:entity_reference_revisions:paragraph',
    +      'label' => $field_title,
    +      'field_name' => 'content',
    +    ];
    +    $this->drupalGet('admin/structure/types/manage/paragraphed_content_demo/fields/add-field');
    +    $this->drupalPostForm(NULL, $edit, t('Save and continue'));
    +    $this->drupalPostForm(NULL, [], t('Save field settings'));
    +    $edit = [
    +      'required' => TRUE,
    +    ];
    +    $this->drupalPostForm(NULL, $edit, 'Save settings');
    

    We have static function ''fieldUIAddNewField'', maybe you can use it.

  6. +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
    @@ -1401,6 +1429,32 @@ class InlineParagraphsWidget extends WidgetBase {
    +   * @param string $mode
    +   *   The mode to look for.
    +   * @return int
    +   *   The number of paragraphs is the given mode.
    

    Separate the @param and @return selections by a blank line.

VladimirMarko’s picture

Status: Needs work » Needs review
StatusFileSize
new5.38 KB
new5.87 KB
new13.32 KB

4. Removed from that class as well.

5. Great suggestion! Implemented.

Implemented everything.

The last submitted patch, 81: empty_required_fields-2788607-81-test-only.patch, failed testing.

The last submitted patch, 81: empty_required_fields-2788607-81-test-only.patch, failed testing.

toncic’s picture

Status: Needs review » Reviewed & tested by the community

Looks fine now.

spadxiii’s picture

Quick test: the error message now indeed shows the field label. But the field itself is not marked as an error.

recrit’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new13.32 KB

re-rolled patch against the latest dev 31fe6df since it was not applying.

miro_dietiker’s picture

Status: Needs review » Fixed

Committed, thx, lots of attributions. :-)

Still authoring @andywhale as the original fixing logic came from him.

Status: Fixed » Closed (fixed)

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

mbm80’s picture

For those who are locked in paragraphs to v1.1 and want to use the patch i had to adapt the 86 patch a bit, just removed the assert in ParagraphsConfigTest.php since the method is missing.

finex’s picture

Hi, the bug is still reproducible on current -dev version. If you have a required field on a paragraph and you save the node the message error is:

This value should not be null

finex’s picture

Category: Feature request » Bug report
Priority: Minor » Normal
ankit agrawal’s picture

I have recently faced this issue with the media library widget used in the media field of the paragraph (v1.12), got resolved after changing the widget to entity browser.