2014-05-07 74 views
0

我對Symfony2 Forms + Entities +驗證過程有些懷疑。拿這個代碼示例(/src/Common/CommonBundle/Resources/config/validation.yml):Symfony2,在YML文件,實體和表單驗證

Common\CommonBundle\Entity\AddressExtraInfo: 
    properties: 
     town: 
      - NotBlank: 
       message: "This value should not be blank" 
      - Length: 
       min: 3 
       max: 50 
       minMessage: "This value should be {{ limit }} or more" 
       maxMessage: "This value should be {{ limit }} or less" 
      - Regex: 
       pattern: "/^[\w\sÑñÁÉÍÓÚáéíóú]+$/" 
       match: false 
       message: "This value should be of type {{ alfanumérico }}" 

現在,

  1. 這種驗證適用於:FormType和實體或只是其中之一?在第二種情況下哪一個?
  2. 我在我的應用程序上使用i18n,如果我使用/vendor/symfony/symfony/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf的翻譯,然後「fr」這些消息將使用法語或將以英語顯示?

這是實體(只是相關的代碼)的樣子:

<?php 

namespace Common\CommonBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 
use Gedmo\Mapping\Annotation as Gedmo; 
use Common\CommonBundle\Model\BaseEntityTrait; 
use Common\CommonBundle\Model\IdentifiedAutogeneratedEntityTrait; 

/** 
* @ORM\Entity 
* @ORM\Table(name="address_extra_info") 
* @Gedmo\SoftDeleteable(fieldName="deletedAt") 
*/ 
class AddressExtraInfo 
{ 

    use IdentifiedAutogeneratedEntityTrait; 
    use BaseEntityTrait; 

    /** 
    * Municipio 
    * 
    * @ORM\Column(type="string", length=255) 
    * @Assert\NotBlank(message="Este valor no debería estar vacío.") 
    */ 
    protected $town; 

    .... 

    public function setTown($town) 
    { 
     $this->town = strip_tags($town); 
    } 

    public function getTown() 
    { 
     return $this->town; 
    } 

    ... 

} 

一個額外的疑問,在這個實體:有必要用strip_tags每組的方法?或學說或Symfony照顧這個?

AddressExtraInfoType.php

<?php 

namespace Common\CommonBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 
use Symfony\Component\Validator\Constraints as Assert; 

class AddressExtraInfoType extends AbstractType 
{ 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add(
       'town', 'text', array(
       'required' => true, 
       'attr' => array(
        'class' => 'address-input wv-tooltip', 
        'style' => 'width:272px;', 
        'placeholder' => 'Municipio *', 
        'tt-placement' => 'right', 
        'validated' => 'validated', 
        'onkeypress' => 'return isAlphaNumeric(event)', 
        'ng-minlength' => '3', 
        'maxlength' => '50', 
        'ng-pattern' => '/^[\w\sÑñÁÉÍÓÚáéíóú]+$/', 
        'wv-err' => 'Este valor debería ser de tipo alfanumérico', 
        'wv-cur' => '', 
        'wv-req' => 'Este valor no debería estar vacío.', 
        'wv-min' => 'Este valor debería ser de 3 ó más' 
      ))); 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(
      array(
       'data_class' => 'Common\CommonBundle\Entity\AddressExtraInfo' 
      ) 
     ); 
    } 

    public function getName() 
    { 
     return 'address_extra_info'; 
    } 

} 

回答

1

1)在symfony的2驗證文檔的最頂端。

「驗證是一種很常見的任務,在Web應用程序中的表單中輸入的數據需要被驗證的數據。在寫入數據庫或傳遞給Web服務之前,還需要進行驗證。 Symfony2附帶了一個Validator組件,該組件使此任務變得簡單且透明。 http://symfony.com/doc/current/book/translation.html#translating-constraint-messages

3)它不會傷害有條標籤:如果設置都正確

http://symfony.com/doc/current/book/validation.html

2)取決於。 Symfony表單只會傳回用戶給出的信息,但用戶可能會輸入標籤,所以如果這是一個問題,請將strip_tags放入。