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

故事扮演 提交于 2019-12-02 13:37:49

问题


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?


回答1:


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;
}



回答2:


/**
* @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.



来源:https://stackoverflow.com/questions/42245953/how-to-manage-multidimensional-array-name-of-fields-at-server-side-validation-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!