0

I have defined Constraints in my Entity as the documentation said like this example bellow :

/**
 * @ORM\Column(type="float", nullable=true)
 * @Assert\Type(type={"float", "integer"}, message="Budget have to be a number")
 */
private $budget;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotNull
 */
private $statut;

But when I send invalid value throw the form, the error message that is send is not the one in my constraints entity but the "default one" : "This value is not valid" instead of "Budget have to be a number" in this example.

/**
 * @Route("/composition/{id}/edition", name="composition_edition")
 */
public function editionComposition(Composition $composition_edit, Request $request, EntityManagerInterface $manager)
{
    $form = $this->createForm(EditionCompositionType::class, $composition_edit);
    $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid())
        {            
            $manager->persist($composition_edit);
            $manager->flush();

            return $this->json([
                'erreurs' => "ok"
            ], 200);
        }
        else
        {
            $errors = array();
            foreach ($form->all() as $child)
            {
                if (!$child->isValid())
                {
                    $errors[$child->getName()] = (String) $form[$child->getName()]->getErrors();
                }
            }

            return $this->json([
                'erreurs' => $errors
            ], 200);
        }
}

Do you know how to have the good error message ?

Benjamin
  • 13
  • 2
  • That looks like a `TransformationException`. Can you look in the symfony profiler and add more details? – msg Apr 21 '20 at 20:20
  • The message inside the profiler in the validation form is the default one ("This value is not valid"), not the one in my entity. Do you want to see something else in the profier ? – Benjamin Apr 22 '20 at 16:23
  • In the Form pane you should be able to drill down to the exception trace like [this one](https://codereviewvideos.com/blog/wp-content/uploads/2018/02/symfony-4-json-api-form-validation-error.png). Can you see any nested exception or just a constraint failure? – msg Apr 22 '20 at 16:48
  • I have the ConstraintViolation and after that a TransformationFailedException with this message : "Unable to reverse value for property path "budget": Number parsing failed: U_PARSE_ERROR" – Benjamin Apr 22 '20 at 17:12
  • I'll take a guess and say this is related to `intl`. Do you have the extension installed or using the symfony package? Maybe a `null` value? It seems to trace back to [this](https://github.com/symfony/symfony/blob/f2f82d1e144797423b61901085b6db3882de542f/src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php#L524). Also take a look at [this question](https://stackoverflow.com/questions/2251290/storing-money-amounts-in-mysql#2251311) just in case – msg Apr 22 '20 at 17:32
  • I have change budget from float to a decimal like your example said it. Now my error message from my constraint entity appear in the profiler but I can't catch it in my controller. And I still have a TransformationFailedException. – Benjamin Apr 24 '20 at 17:49

0 Answers0