2014-09-13 52 views
-1

我的文件validators.en.yml中有幾個驗證消息:Symfony2中得到驗證消息

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

原諒我的英語不好,我用翻譯者。

回答

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

不過,我建議你閱讀本:如果你想跟隨Symfony的最佳實踐http://symfony.com/doc/2.3/cookbook/validation/custom_constraint.html

,你應該創建一個自定義約束類/驗證器並將驗證邏輯移入其中。

致以問候