Search code examples
symfonya2lix-translation

How to manage multidimensional array name of fields at server side validation with a2lix translation extension?


I am having two tables content_page and content_page_translations.

When I build the form, form field name generated is like this: content_page[translations][en][pageTitle]

Now, let me know how to manage multidimensional array server-side validation with this extension?


Solution

  • Please have a look at my solution with symfony 3.

    create validation.yml file in config directory containing following lines of code:

    AppBundle\Entity\ContentPages:
    properties:
        status:
            - NotBlank: 
                message: cms.status.not_blank
        cmsTranslations:
            - Valid: ~
    
    AppBundle\Entity\ContentPagesTranslation:
    properties:
        pageTitle:
            - NotBlank: 
                message: cms.page_title.not_blank
            - Length:
                max: 100
        description:
            - NotBlank: ~
            - Length:
                min: 50        
        metaKeywords:
            - NotBlank: ~        
        metaDescription:
            - NotBlank: ~
    

    In the controller file's method you can get validation with below code:

    $entity = new ContentPages();
    
        $validator = $this->get('validator');
        $errors = $validator->validate($entity);
        if (count($errors) > 0) {
            $errorsString = (string) $errors;
            return new Response($errorsString);
        }
    

    Entity file changes: ContentPages.php

    /**
     * @ORM\OneToMany(
     *   targetEntity="ContentPagesTranslation",
     *   mappedBy="object",
     *   cascade={"persist", "remove"}
     * )
     */
    private $cmsTranslations;
    public function __construct() {
        $this->cmsTranslations = new ArrayCollection();
    }
    public function getTranslations() {
        return $this->cmsTranslations;
    }