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

717 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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;
}
0
On
/**
* @Assert\Valid
*/
protected $translations;

Is not working for you? Just curious. Because it should be the official answer, as I understand it. But this doesn't work for me when I submit all translatable fields empty. It works only when any of the fields is submitted.