'Use Symfony Validator in a loop of multiple elements

I hope you're doing well.

I am encountering a problem developing my API with API Platform on Symfony.

The goal of my project is to analyze excel files and sub sheets related to it. Everything is good at the moment and saved to my MySql database as I expected.

BUT, the big problem is the validation. I am using regex Validator in the entity. The validation itself is working but not exactly as I want.

As I am treating a lot of rows in my excel, I want to detect an error at a specific row, store the error, and return a message with all errors encountered at the end of the treatment of all rows.

But, at the first constraint violation, my code is stopped, and I don't know which entity is concerned as in my error log, I don't have any ID... And I don't want that the validator stops my code like that; the best would be to have notice and be non-blocking.

Example of the error returned :

{
    "@context": "/api/contexts/ConstraintViolationList",
    "@type": "ConstraintViolationList",
    "hydra:title": "An error occurred",
    "hydra:description": "myErrorMessage",
    "violations": [
        {
            "propertyPath": "myPropertyName",
            "message": "myPropertyErrorDescription",
            "code": "de1e3db3-5ed4-4941-aae4-59f3667cc3a3"
        }
    ]
}

Each sub sheet on my excel file is related to a lang. For example, I Have "ENGLISH," "FRENCH," "ITALIAN," etc... This is for translating each product into each lang.

The function concerned :

public function importLangProduct($acceptedExcelCode, $productsLanguagesSheet, $row_common, $codeLang, $i) {
        if (!array_key_exists('B', $row_common))
            throw new BadRequestException("You need at least an EAN for a product");
        if (!isset($productsLanguagesSheet[$i]))
            return;
        if (!array_key_exists('B', $productsLanguagesSheet[$i]))
            return;
        $product_lang = $this->entityManager->getRepository(ProductLanguage::class)->findOneBy(['ean' => $row_common['B'], 'language' => $codeLang]);
        if (null === $product_lang)
            $product_lang = new ProductLanguage();
        $product_lang->setEan($row_common['B']);
        $product_lang->setLanguage($codeLang);
        $product_lang->setName($row_lang['E']);
        $product_lang->setDescription($row_lang['F']);
        $this->validator->validate($product_lang); //the problem
        $this->entityManager->merge($product_lang);
        $this->entityManager->flush();
    }

A part of my entity file as you will be able to see how I manage my regex :

/**
     * @ORM\Column(type="text", nullable=true, name="name")
     * @ApiProperty()
     * @Assert\Regex(
     *     pattern="/.{10,400}/",
     *     match=true,
     *     message="Your product name is not correct. It should be a value from 10 to 400 characters",
     *     normalizer="trim"
     * )
     */
    private $name;

    /**
     * @ORM\Column(type="text", nullable=true, name="description")
     * @ApiProperty()
     * @Assert\Regex(
     *     pattern="/.{10,2000}/",
     *     match=true,
     *     message="Your product description is not correct. It should be a value from 10 to 2000 characters",
     *     normalizer="trim"
     * )
     */
    private $description;

Sorry for my bad English, thank you in advance ! :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source