Locastic / ApiPlatformTranslationBundle

Translation bundle for ApiPlatform based on Sylius translation
MIT License
84 stars 27 forks source link

The Symfony validator don't with this bundle #44

Closed kevinG73 closed 3 years ago

kevinG73 commented 3 years ago

Hello , I tried to use Constraints such as : length or NotBlank but it seems be ignored

AreaTranslation.php


/**
 * @ORM\Entity()
 */
class AreaTranslation extends AbstractTranslation
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"Area_read","Area_write"})
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Area", inversedBy="translations")
     * @Assert\NotNull
     */
    protected $translatable;

    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     * @Assert\Length(max=1500)
     * @Groups({"Area_read","Area_write","translations"})
     */
    private $name;

    /**
     * @ORM\Column(type="string")
     * @Assert\NotBlank
     * @Assert\Language
     * @Groups({"Area_read","Area_write","translations"})
     */
    protected $locale;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }
}
orangevinz commented 2 years ago

If you want to validate nested items, you must add the @Assert\Valid at the property level this way


/**
 * @ORM\Entity()
 */
class Area extends AbstractTranslatable
{
     /**
     * @Assert\Valid
     * @ORM\OneToMany(targetEntity="App\Entity\AreaTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
     * @Groups({"your_custom_group_read", "your_custom_group_write", "translations"})
     */
    protected $translations;
}

Reference: https://stackoverflow.com/a/60431468/12750122