a2lix / TranslationFormBundle

Ease translations with some dedicated Symfony form types
https://a2lix.fr/bundles/translation-form
MIT License
330 stars 140 forks source link

validation not workings on entity translation #356

Closed LeTo34 closed 2 years ago

LeTo34 commented 3 years ago

Hi, I'm trying to manage translations about a "Page" entity. I have NotBlank constraint on "title" field in the "PageTranslation" entity but its not working... When I submit the form with empty values, it's valid... If you have an idea about where is the problem... you're welcome ! I'm using SF 4.4

Here is the code

Page.php

<?php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PageRepository")
 * @ORM\Table(name="page")
 */
class Page implements TranslatableInterface
{
    use Common\IdTrait;
    use TranslatableTrait;
    use TimestampableEntity;

    /**
     * @Assert\Valid()
     */
    protected $translations;

    public function __call($method, $arguments)
    {
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), $method);
    }

}

PageTranslation.php

<?php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="page_translations")
 */
class PageTranslation implements TranslationInterface
{
    use Common\IdTrait;
    use Common\SeoTrait;
    use TranslationTrait;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", nullable=true, length=150)
     * @Assert\NotBlank()
     */
    protected $title;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", nullable=true)
     */
    protected $description;

    /**
     * @Gedmo\Slug(fields={"title"})
     * @ORM\Column(length=128, unique=true)
     */
    private $slug;

    /**
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param string $title
     * @return PageTranslation
     */
    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param string $description
     * @return PageTranslation
     */
    public function setDescription($description)
    {
        $this->description = $description;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * @param mixed $slug
     * @return PageTranslation
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;
        return $this;
    }
}

PageType.php

<?php

namespace App\Form;

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use App\Entity\Page;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PageType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder
            ->setMethod('POST')
            ->add('save', SubmitType::class, array(
                'label' => 'Valider',
            ))
        ;

        $builder
            ->add('translations', TranslationsType::class, [
            'fields' => [
                'title' => [
                    'field_type' => TextType::class,
                    'locale_options' => [
                        'en' => [
                            'attr' => ['class' => 'form-control-lg form-control-solid'],
                        ],     
                        'fr' => [
                            'attr' => ['class' => 'form-control-lg form-control-solid'],
                        ],     
                    ]
                ],
            ],
            'excluded_fields' => ['description', 'slug']            // [2]
        ]);

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Page::class,
            'validation_groups' => function (FormInterface $form) {
                return ['Default'];
            },
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'page_form';
    }
}

PageController.php

.................
/**
     * @Route("/page/add", name="page_add")
     * @Template("page/add.html.twig")
     * @param Request $request
     * @param EntityManagerInterface $em
     * @return array|Response
     */
    public function add(Request $request, EntityManagerInterface $em)
    {

        $page = new Page();

        $form = $this->createForm(PageType::class, $page, [
            'attr' => [
                'novalidate' => 'novalidate'
            ]
        ]);
        $form->handleRequest($request);

        if ($form->isSubmitted()) {

            if ($form->isValid()) {

                $em->persist($page);
                $em->flush();

                ..................................

            } else {

                .................
            }

        }

        return [
            'page'      => $page,
            'form'      => $form->createView()
        ];
    }
....................

a2lix.yaml

a2lix_translation_form:
  locale_provider: default       
  locales: [fr,en]      
  default_locale: 'fr'             
  required_locales: [fr,en]         
  templating: "@A2lixTranslationForm/bootstrap_4_layout.html.twig"      
tchapi commented 2 years ago

Hi

Did you try to assert both fields from the TranslatableTrait ?:


    /**
     * @var Collection<TranslationInterface>
     *
     * @Assert\Valid()
     */
    protected $translations;

    /**
     * @see mergeNewTranslations
     *
     * @var Collection<TranslationInterface>
     * @Assert\Valid()
     */
    protected $newTranslations;