简体   繁体   中英

Symfony2 form error translation

I am using symfony2 validation in my forms with the validation.yml and I tried to validate it all with the validator.{lang}.yml file.

My locale is set to pt_BR and the validator file is located at: {Bundle}/Resources/translations/validators.pt_BR.yml

Also, the validation file of the same bundle is at: {Bundle}/Resources/config/validation.yml

Still, even after I clear the cache the translated messages don't appear.

My validation file:

NC8Digital\CRMBundle\Entity\Usuario:
properties:
    id:
        - Range:
            min: 1
            minMessage: "UserBundle.Usuario.id"
            invalidMessage: "UserBundle.Usuario.id"
        - Type:
            type: numeric
            message: "UserBundle.Usuario.id"
    nome:
        - NotBlank:
            message: "UserBundle.Usuario.nome.not_blank"
        - Length:
            min: 3
            max: 90
            minMessage: "UserBundle.Usuario.nome.min_length"
            maxMessage: "UserBundle.Usuario.nome.max_length"
    sobrenome:
        - NotBlank:
            message: "UserBundle.Usuario.sobrenome.not_blank"
        - Length:
            min: 3
            max: 90
            minMessage: "UserBundle.Usuario.sobrenome.min_length"
            maxMessage: "UserBundle.Usuario.sobrenome.max_length"
    cpf:
        - NotBlank:
            message: "UserBundle.Usuario.cpf.not_blank"
        - NC8Digital\CRMBundle\Validator\Constraints\Cpf:
            message: "UserBundle.Usuario.cpf.cpf"
        - Length:
            min: 14
            max: 14
            exactMessage: "UserBundle.Usuario.cpf.exact_message"
    rg:
        - NotBlank:
            message: "UserBundle.Usuario.rg.not_blank"
        - Length:
            min: 5
            max: 25
            minMessage: "UserBundle.Usuario.rg.min_length"
            maxMessage: "UserBundle.Usuario.rg.max_length"
    celular:
        - Length:
            min: 10
            max: 15
            minMessage: "UserBundle.Usuario.celular.min_length"
            maxMessage: "UserBundle.Usuario.celular.max_length"
        - NC8Digital\CRMBundle\Validator\Constraints\Celular:
            message: "UserBundle.Usuario.celular.celular"
    telefone:
        - NotBlank:
            message: "UserBundle.Usuario.telefone.not_blank"
        - Length:
            min: 10
            max: 15
            minMessage: "UserBundle.Usuario.telefone.min_message"
            maxMessage: "UserBundle.Usuario.telefone.max_message"
        - NC8Digital\CRMBundle\Validator\Constraints\Telefone:
            message: "UserBundle.Usuario.telefone.telefone"
    sexo:
        - NotBlank:
            message: "UserBundle.Usuario.sexo.not_blank"
    data_nascimento:
        - Date:
            message: UserBundle.Usuario.data_nascimento.not_blank
    tipousuario:
        - NotBlank:
            message: "UserBundle.Usuario.tipo_usuario.not_blank"
    endereco:
        - Valid: ~
    login:
        - Valid: ~

My translation file:

UserBundle:
Usuario:
    id: Código do usuário inválido.
    nome:
        not_blank: "Insira o nome."
        min_length: "Seu nome deve ter no mínimo 3 caracteres"
        max_length: "Seu nome deve ter no máximo 90 caracteres"
    sobrenome:
        not_blank: "Insira o sobrenome."
        min_length: "Seu sobrenome deve ter no mínimo 3 caracteres"
        max_length: "Seu sobrenome deve ter no máximo 90 caracteres"
    cpf:
        not_blank: "Insira o CPF."
        cpf: "Insira um CPF válido"
        exact_message: "Seu CPF deve ter 14 caracteres (incluíndo pontos e traços)."
    rg:
        not_blank: "Insira o RG."
        min_length: "O RG deve ter no mínimo 5 caracteres"
        max_length: "O RG deve ter no máximo 25 caracteres"
    celular:
        min_length: "O celular deve ter no mínimo 10 caracteres (sem separadores)."
        max_length: "O celular deve ter no máximo 15 caracteres (incluindo separadores)."
        celular: "Insira um celular válido."
    indicacoes_dia:
        min_range: "A quantidade de indicacões por dia deve ser no mínimo 0."
        max_range: "A quantidade de indicacões por dia deve ser no máximo {{ limit }}."
        type_message: "Quantidade de indicações por dia inválida."
    telefone:
        not_blank: "Insira o Telefone."
        min_message: "O telefone deve ter no mínimo 10 caracteres (sem separadores)"
        max_message: "O telefone deve ter no máximo 15 caracteres (incluindo separadores)."
        telefone: "Insira um telefone válido."
    sexo:
        not_blank: "Insira o Gênero."
    data_nascimento:
        not_blank: "Insira uma data de nascimento válida."
    tipo_usuario:
        not_blank: "Insira o Tipo de usuário."

I've added the novalidate attribute for disabling HTML5 validation. And your problem will be this: minMessage: "UserBundle.Usuario.id" , remove " and i think it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM