如何使用 a2lix 翻译扩展在服务器端验证时管理字段的多维数组名称? [英] How to manage multidimensional array name of fields at server side validation with a2lix translation extension?

查看:18
本文介绍了如何使用 a2lix 翻译扩展在服务器端验证时管理字段的多维数组名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表 content_pagecontent_page_translations.

当我构建表单时,生成的表单字段名称是这样的:content_page[translations][en][pageTitle]

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?

推荐答案

请看看我的 symfony 3 解决方案.

Please have a look at my solution with symfony 3.

在包含以下代码行的配置目录中创建validation.yml文件:

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

实体文件更改:ContentPages.php

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

这篇关于如何使用 a2lix 翻译扩展在服务器端验证时管理字段的多维数组名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆