Skip to content

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

@bam1to

Description

@bam1to

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions