Modules display ID validation does not work with Paragraphs anymore

Created on 20 August 2024, 8 months ago

Problem/Motivation

The new display id validation does not work when the viewsreference field is in a paragraph. When saving the node the browser does the validation. When using the 'novalidate' attribute on the node form, the form can be saved without being validated.
This wasn't a problem before when ViewsreferenceItem::isEmpty() was validating it.

In the latest version (2.0-beta9) the module checks for $field_values[0]['target_id'] and $field_values[0]['display_id'] which do not exisits when using a paragraph.
With paragraphs it's $field_values[0]['subform'][][0]['target_id'] and $field_values[0]['subform'][][0]['display_id'].
It would be great if paragraphs would be supported again.

I'm using Drupal 10.3.2 and Viewsreference 2.0-beta9.

Steps to reproduce

1. Install the Paragraphs module and the Views Reference Field Module
2. Create a paragraph with a viewsreference field
3. Add the paragraph to a content type like 'basic page'
4. Create a new content, fill out/select a view, leave display empty
(5. Add 'novalidate' attribute to form element, to see that it will save without validation when the browser does not validate it)
6. Save

๐Ÿ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany lmoeni

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @lmoeni
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lmoeni
  • First commit to issue fork.
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom scott_euser

    Just looking at this a bit, I don't think isEmpty() was actually validating though. There is a validate() method on the grantparent class of ViewsReferenceItem, so perhaps we should be extending that method.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lmoeni

    I think I worded it incorrectly. The isEmpty() method was used to check whether the display_id was empty before the validation took place.
    Which class do you mean? I couldn't find a validate() method in the class that ViewsReferenceItem extends.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia dev2.addweb

    nilesh.addweb โ†’ made their first commit to this issueโ€™s fork.

  • Merge request !67Added Subform validation. โ†’ (Open) created by Unnamed author
  • Pipeline finished with Success
    7 months ago
    Total: 239s
    #286122
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom scott_euser

    Thanks for opening MR, is this ready for review?

  • Status changed to Needs review 7 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia dev2.addweb

    Yes, ready for review. we can use validateDisplayId method which is present in ViewsReferenceTrait.php.
    Please review MR !67.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom scott_euser

    Thanks for the progress on this! Tested this out, couple issues:

    • This only works if the paragraph is the first one
    • The validation from setErrorByName doesn't actually set the error to the display id field
  • Pipeline finished with Failed
    6 months ago
    Total: 226s
    #336340
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia dev2.addweb

    @scott_euser, Thanks for review, I have updated MR.

  • Pipeline finished with Failed
    18 days ago
    Total: 268s
    #470931
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lmoeni

    I was getting the following warning:

    Warning: Undefined array key "subform" in Drupal\viewsreference\Plugin\Field\FieldWidget\ViewsReferenceSelectWidget::validateDisplayId() (line 216 of modules/contrib/viewsreference/src/Plugin/Field/FieldWidget/ViewsReferenceTrait.php).
    Drupal\viewsreference\Plugin\Field\FieldWidget\ViewsReferenceSelectWidget::validateDisplayId() (Line: 53)
    Drupal\viewsreference\Plugin\Field\FieldWidget\ViewsReferenceSelectWidget::validateElement()

    That's why I added isset() to $field_value['subform'].

    Besides that it workes for me. But I think we could try a different approach by using the Validation API. I will open a new MR with the changes I made. It does not work 100% correctly right now. It works great with the autocomplete field. When you use the select list the error shows up but all fields of the widget somehow show the red border when the error appears. I'm not sure if this might be a core problem or if the way the widget is defined might be the problem or something else. Maybe I'm missing something. There is not much documentation on this.
    It would be great if someone could take a look at this. We should also write tests for this scenario.

  • Merge request !87Change the validation for the display id โ†’ (Open) created by lmoeni
  • Status changed to Needs work 18 days ago
  • Pipeline finished with Failed
    18 days ago
    Total: 269s
    #470963
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lmoeni

    In case anyone needs a working patch for this issue, I created one from the first merge request.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany lmoeni
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom scott_euser

    Before I retest would be good to:

    1. Hide the branch that isn't in use (its confusing to have 2)
    2. Fix test coverage

    Thanks!

Production build 0.71.5 2024