Skip to content

fix(hydra): Fix server-side validation errors do not show up #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025

Conversation

slax57
Copy link
Contributor

@slax57 slax57 commented Apr 8, 2025

Q A
Branch? main
Tickets
License MIT
Doc PR

Issue

Contrary to what the docs say, server-side validation errors do not appear beneath the related input.

At least when using <HydraAdmin> with API Platform 4.1 using Symfony.

Solution

Fix the getSubmissionErrors implementation.

Real-world tests show that validation errors are formatted like so:

  "https://localhost/docs.jsonld#ConstraintViolation/violations": [
    {
      "https://localhost/docs.jsonld#ConstraintViolation/code": [
        {
          "@value": "2881c032-660f-46b6-8153-d352d9706640"
        }
      ],
      "https://localhost/docs.jsonld#ConstraintViolation/message": [
        {
          "@value": "This value is neither a valid ISBN-10 nor a valid ISBN-13."
        }
      ],
      "https://localhost/docs.jsonld#propertyPath": [
        {
          "@value": "isbn"
        }
      ]
    }
  ]

whereas the existing code seems to expect them like so:

  "https://localhost/docs.jsonld#ConstraintViolation/violations": [
    {
      "https://localhost/docs.jsonld#code": [
        {
          "@value": "2881c032-660f-46b6-8153-d352d9706640"
        }
      ],
      "https://localhost/docs.jsonld#message": [
        {
          "@value": "This value is neither a valid ISBN-10 nor a valid ISBN-13."
        }
      ],
      "https://localhost/docs.jsonld#propertyPath": [
        {
          "@value": "isbn"
        }
      ]
    }
  ]

The new implementation I suggest is compatible with both.

How to Test

A unit test was added.

Besides, this can be tested in Storybook.

  1. Edit the Book.php file to add a constraint to the $isbn field: #[Assert\Isbn]
  2. Start the api and storybook: docker compose up
  3. Browse http://localhost:3000/?path=/story/admin-basic--admin
  4. Try to edit a book with an invalid ISBN (any number really)
  5. You should see the following error underneath the input

Screenshot_20250408_151224

@slax57 slax57 added the RFR label Apr 8, 2025
@djhi djhi merged commit 46f97bd into main Apr 9, 2025
4 checks passed
@slax57 slax57 added this to the 4.0.7 milestone May 16, 2025
@slax57 slax57 deleted the fix/server-side-validation-errors branch May 16, 2025 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy