Symfony2获取验证消息

时间:2014-09-13 15:18:49

标签: symfony

我在文件validators.en.yml中有一些验证消息:

soporte.nombre.not_blank: The name cannot be empty
soporte.price.is_integer: The price should be integer
soporte.not_repeat: soporte cannot be repeat

我有效地对数据库进行查询:

$validarPorNombreAndTipo = $this->crud->findOneBy(
    $soporte, array('nombre' => $soporte->getNombre(),                  
                    'tipo' => $object->tipo
    )
);
if ($validarPorNombreAndTipo){
         $error= //here i need to get soporte.not_repeat that is on file validators.en.yml;
        }
对不起我的英语不好,我使用了翻译。

1 个答案:

答案 0 :(得分:0)

如果您只想获得翻译,则此代码应该有效:

$validarPorNombreAndTipo = $this->crud->findOneBy(
$soporte, array('nombre' => $soporte->getNombre(),                  
                'tipo' => $object->tipo
    )
);

if ($validarPorNombreAndTipo){
    $error= $this->get('translator')->trans('soporte.not_repeat', array(), 'validators');
    // Third param is the translation domain (first part of the translation file)
}

但是,我建议你阅读:http://symfony.com/doc/2.3/cookbook/validation/custom_constraint.html

如果您想遵循Symfony最佳实践,您应该创建一个自定义约束类/ Validator并将验证逻辑移入其中。

祝你好运

相关问题