Skip to content
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

NoSuspiciousCharacters constraint doesn't allow to change the error messages #51715

Closed
bam1to opened this issue Sep 21, 2023 · 1 comment · Fixed by #51726
Closed

NoSuspiciousCharacters constraint doesn't allow to change the error messages #51715

bam1to opened this issue Sep 21, 2023 · 1 comment · Fixed by #51726

Comments

@bam1to
Copy link
Contributor

bam1to commented Sep 21, 2023

Symfony version(s) affected

6.3.4

Description

Error messages doesn't change while use construct parameters to change them
This applies to the following parameters:
restrictionLevelMessage,
invisibleMessage,
mixedNumbersMessage,
hiddenOverlayMessage
And maybe more

How to reproduce

  1. Create a class with fields that contains NoSuspiciousCharacters constraint.
  2. Change the message text through parameters (for example through: restrictionLevelMessage)
  3. Try to validate this class and see what message have come (I use the suspicious example from symfony doc page: "ѕymfony.com")
    For example:
    UserPersonalDataDto.php
class UserPersonalDataDto {
    #[Assert\NoSuspiciousCharacters(restrictionLevelMessage: "dummyError")]
    private string $firstName;
// getters/setters etc
}

UserController.php

class UserController extends AbstractController {
    #[Route('/dummy-controller')]
    public function personalData() 
    {
        $personalData = $serializer->deserialize($request->getContent(), InputUserPersonalDataDto::class, 'json');
          
        $violations = $validator->validate($personalData);
        if ($violations->count() > 0) {
            $violationsArr = $violations->getIterator()->getArrayCopy();
            return $this->json($violationsArr);
        }
        return $this->json('');
    }
}
  1. Should be "dummyError" and I get "This value contains characters that are not allowed by the current restriction-level."

Possible Solution

To change the code of construct of the NoSuspiciousCharacters class from:

        $this->restrictionLevelMessage ??= $restrictionLevelMessage;
        $this->invisibleMessage ??= $invisibleMessage;
        $this->mixedNumbersMessage ??= $mixedNumbersMessage;
        $this->hiddenOverlayMessage ??= $hiddenOverlayMessage;
        $this->checks ??= $checks;
        $this->restrictionLevel ??= $restrictionLevel;
        $this->locales ??= $locales;

to:

    $this->restrictionLevelMessage = $restrictionLevelMessage ?? $this->restrictionLevelMessage;
    $this->invisibleMessage = $invisibleMessage ?? $this->invisibleMessage;
    $this->mixedNumbersMessage = $mixedNumbersMessage ?? $this->mixedNumbersMessage;
    $this->hiddenOverlayMessage = $hiddenOverlayMessage ?? $this->hiddenOverlayMessage;
    $this->checks = $checks ?? $this->checks;
    $this->restrictionLevel = $restrictionLevel ?? $this->restrictionLevel;
    $this->locales = $locales ?? $this->locales;

Additional Context

No response

@bam1to bam1to added the Bug label Sep 21, 2023
@derrabus
Copy link
Member

Please send a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants