Symfony2學習筆記之資料校正

來源:互聯網
上載者:User

標籤:blog   http   java   使用   os   io   strong   檔案   


校正在web應用程式中是一個常見的任務。資料輸入到表單需要被校正。資料在被寫入資料庫之前或者傳入一個webservice時也需要被校正。

Symfony2 配備了一個Validator 組件,它讓校正工作變得簡單易懂。該組件是基於JSR303 Bean校正規範。一個Java規範用在PHP中。

 

基本驗證
理解校正的最好方法是看它的表現。首先,假設你已經建立了一個用於你應用程式某個地方的PHP對象。

//src/Acme/BlogBundle/Entity/Author.phpnamespace Acme\BlogBundle\Entity;class Author{    public $name;}

到現在為止,它只是個服務於你應用程式的某些目的的普通的類。而校正的目的就是要告訴你對象的資料是否合法。為了這個目的,你需要配置一個對象必須遵守規則或者約束列表來讓自己的資料合法。這些規則可以被描述成多種不同的格式的(比如,YAML,XML,類聲明或者PHP)。比如,我們保證屬性$name不可為空,來添加下面的規則:
YAML格式:

# src/Acme/BlogBundle/Resources/config/validation.ymlAcme\BlogBundle\Entity\Author:    properties:         name:            - NotBlank: ~

類聲明格式:

// src/Acme/BlogBundle/Entity/Author.phpuse Symfony\Component\Validator\Constraints as Assert;class Author{   /**    * @Assert\NotBlank()    */    public $name;}

XML格式:

<!-- src/Acme/BlogBundle/Resources/config/validation.xml --><?xml version="1.0" encoding="UTF-8" ?><constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">    <class name="Acme\BlogBundle\Entity\Author">        <property name="name">            <constraint name="NotBlank" />        </property>    </class></constraint-mapping>

PHP代碼格式:

// src/Acme/BlogBundle/Entity/Author.phpuse Symfony\Component\Validator\Mapping\ClassMetadata;use Symfony\Component\Validator\Constraints\NotBlank;class Author{   public $name;      public static function loadValidatorMetadata(ClassMetadata $metadata)   {       $metadata->addPropertyConstraint(‘name‘, new NotBlank());   }}

Protected和private屬性以及getter方法也都可以被校正。


使用validator服務:
接下來,使用validator服務的validate方法來真正的校正Author對象。 validator的工作很簡單:讀取一個類的約束規則來校正一個對象的資料是否符合這些規則約束。如果校正失敗,一個錯誤數組將被返回。現在我們在一個controller中來執行它:

use Symfony\Component\HttpFoundation\Response;use Acme\BlogBundle\Entity\Author;//...public function indexAction(){   $author = new Author();   //... 對$auother對象做些什麼      $validator = $this->get(‘validator‘);   $errors = $validator->validate($author);   if(count($errors) >0){     return new Response(print_r($errors, true));   }else{     return new Response(‘The author is valid! Yes!‘);   }}

如果$name 屬性為空白,你將看到下面的錯誤資訊:

Acme\BlogBundle\Author.name:     This value should not be blank

如果你為$name屬性插入一個值,那麼你會獲得快樂的成功資訊。

大多數時候,你不需要直接跟validator服務交流或者根本不需要擔心列印出錯誤來。
大多數情況下,你將在處理提交表單資料時間接使用校正。

你也可以傳遞一個錯誤資訊集合到一個模版:

if(count($errors)>0){   return $this->render(‘AcmeBlogBundle:Author:validate.html.twig‘,array(        ‘errors‘ => $errors,   ));}else{  //...}

在模版中,你可以根據需要精確的輸出錯誤清單:
Twig格式:

{# src/Acme/BlogBundle/Resources/views/Author/validate.html.twig #}<h3>The author has the following errros</h3><ul>{% for error in errors %}    <li>{{ error.message }}</li>{% endfor %}</ul>

 

 

校正和表單
validator服務可以被用於任何時候校正任何對象。 事實上,你將經常在處理表單時間接使用validator。Symfony的表單類庫間接使用validator服務來在資料被提交和綁定後校正底層對象。對象違反約束資訊將被轉化到FieldError對象,該對象可以很容易的被展示在你的表單中。在一個controller中的傳統表單提交流程如下:

use Acme\BlogBundle\Entity\Author;use Acme\BlogBundle\Form\AuthorType;use Acme\Component\HttpFoundation\Request;//...public function updateAction(Request $request){    $author = new Acme\BlogBundle\Entity\Author();    $form = $this->createForm(new AuthorType(),$author);    if($request->getMethod() ==‘POST‘){       $form->bindRequest($request);        if($form->isvalid()){          //對$author做一些操作          return $this->redirect($this->generateUrl(‘...‘));       }     }     return $this->render(‘BlogBundle:Author:form.html.twig‘,array(         ‘form‘ => $form->createView(),     ));}

 


配置:
Symfony2 的validator預設情況下是可用的。但是如果你使用了生命方法來指定你的約束,那麼你需要顯式的開啟聲明功能:
YAML格式:

# app/config/config.ymlframework:   validation: {enable_annotations: true }

XML格式:

<!-- app/config/config.xml --><framework:config>   <framework:validation enable-annotations="true" /></framework:config>

PHP代碼格式:

// app/config/config.php$contianer->loadFromExtension(‘framework‘,array(‘validation‘=> array(     ‘enable_annotations‘=>true,)));

 


約束規則
Validator是設計了用來按照約束規則校正對象的。為了校正一個對象,只需要映射一個或者多個約束到它要校正的類然後把它傳遞給validator服務即可。

本質上,一個約束就是一個簡單的PHP對象,它可以產生一個決斷語句。 在現實生活中,一個約束可以是"蛋糕不能烤焦了" 這樣的規則約束。在Symfony2中,約束都差不多:他們決斷某個條件是否成立。給定一個值,約束會告訴你這個值是否遵守了你的約束規則。

 

Symfony2 支援的約束規則
首先是基礎約束規則:使用他們來決斷非常基本的事,比如你對象屬性的值或者方法的傳回值。
NotBlank,Blank,NotNull,Null,True,False,Type

字串約束:Email,MinLength,MaxLength,Url,Regex,Ip等
數字約束:Max,Min
日期約束:Date,DateTime和Time
集合約束:Choice,Collection,UniqueEntity,Language,Locale和Country等。
檔案約束:File,Image
其它約束:Callback,All,Valid
你也可以建立自己的自訂約束。


約束配置:
一些約束,比如NotBlank,很簡單,但是其它的比如Choice約束,有許多配置項需要設定。假設Author類有另外一個屬性,gener可以被設定為”male"或者"female":
YAML格式:

# src/Acme/BlogBundle/Resources/config/validation.ymlAcme\BlogBundle\Entity\Author:    properties:         gener:            - Choice: { choices: [male, female], message: Choos a valid gender. }

類聲明格式:

// src/Acme/BlogBundle/Entity/Author.phpuse Symfony\Component\Validator\Constraints as Assert;class Author{   /**    * @Assert\Choice(    *     choices = {"male","female"},    *     message = "Choose a valid gender."    * )    */    public $gender;}

XML格式:

<!-- src/Acme/BlogBundle/Resources/config/validation.xml --><?xml version="1.0" encoding="UTF-8" ?><constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">    <class name="Acme\BlogBundle\Entity\Author">        <property name="gender">            <constraint name="Choice">                <option name="choices">                    <value>male</value>                    <value>female</value>                </option>                <option name="message">Choose a valid gender.</option>            </constraint>        </property>    </class></constraint-mapping>

PHP代碼格式:

// src/Acme/BlogBundle/Entity/Author.phpuse Symfony\Component\Validator\Mapping\ClassMetadata;use Symfony\Component\Validator\Constraints\NotBlank;class Author{    public $gender;    public static function loadValidatorMetadata(ClassMetadata $metadata)    {        $metadata->addPropertyConstraint(‘gender‘, new Choice(array(            ‘choices‘ => array(‘male‘, ‘female‘),            ‘message‘ => ‘Choose a valid gender.‘,        )));    }}

一個約束的選項通常都是通過一個數組來傳遞的。有些約束也允許你傳遞一個值。"default"在數組中是可選的。在Choice約束時,choices選項就可以通過這種方式指定。
YAML格式:

# src/Acme/BlogBundle/Resources/config/validation.ymlAcme\BlogBundle\Entity\Author:    properties:        gender:            - Choice: [male, female]

類聲明格式:

// src/Acme/BlogBundle/Entity/Author.phpuse Symfony\Component\Validator\Constraints as Assert;class Author{    /**     * @Assert\Choice({"male", "female"})     */    protected $gender;}

 

XML格式:

<!-- src/Acme/BlogBundle/Resources/config/validation.xml --><?xml version="1.0" encoding="UTF-8" ?><constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">    <class name="Acme\BlogBundle\Entity\Author">        <property name="gender">            <constraint name="Choice">                <value>male</value>                <value>female</value>            </constraint>        </property>    </class></constraint-mapping>

PHP格式:

// src/Acme/BlogBundle/Entity/Author.phpuse Symfony\Component\Validator\Mapping\ClassMetadata;use Symfony\Component\Validator\Constraints\Choice;class Author{    protected $gender;    public static function loadValidatorMetadata(ClassMetadata $metadata)    {        $metadata->addPropertyConstraint(‘gender‘, new Choice(array(‘male‘, ‘female‘)));    }}

 


約束目標
約束可以被用於一個類的屬性或者一個公用的getter方法。屬性約束最常用也最簡單,而公用的getter方法約束則允許你指定一個複雜的約束規則。

屬性約束:
校正類的屬性石一個最常規的校正技術。Symfony2允許你校正private,protected或者public屬性。下面代碼顯示如何配置Author對象的$firstName屬性至少有3個字元:
YAML格式:

# src/Acme/BlogBundle/Resources/config/validation.ymlAcme\BlogBundle\Entity\Author:    properties:        firstName:            - NotBlank: ~            - MinLength: 3

類聲明格式:

// Acme/BlogBundle/Entity/Author.phpuse Symfony\Component\Validator\Constraints as Assert;class Author{    /**     * @Assert\NotBlank()     * @Assert\MinLength(3)     */    private $firstName;}

XML格式:

<!-- src/Acme/BlogBundle/Resources/config/validation.xml --><class name="Acme\BlogBundle\Entity\Author">    <property name="firstName">        <constraint name="NotBlank" />        <constraint name="MinLength">3</constraint>    </property></class>

PHP代碼格式:

// src/Acme/BlogBundle/Entity/Author.phpuse Symfony\Component\Validator\Mapping\ClassMetadata;use Symfony\Component\Validator\Constraints\NotBlank;use Symfony\Component\Validator\Constraints\MinLength;class Author{    private $firstName;    public static function loadValidatorMetadata(ClassMetadata $metadata)    {        $metadata->addPropertyConstraint(‘firstName‘, new NotBlank());        $metadata->addPropertyConstraint(‘firstName‘, new MinLength(3));    }}

 


Getters
約束也可以應用於一個方法的傳回值。Symfony2 允許你添加一個約束到任何"get"或者 "is"開頭的public方法。該技術的好處是允許你動態校正你的對象。比如,假設你想確認密碼欄位不匹配使用者的first name(因為安全原因)。你可以通過建立一個idPasswordLegal 方法,然後決斷這個方法必須返回true:
YAML格式:

# src/Acme/BlogBundle/Resources/config/validation.ymlAcme\BlogBundle\Entity\Author:    getters:        passwordLegal:            - "True": { message: "The password cannot match your first name" }

類聲明格式:

// src/Acme/BlogBundle/Entity/Author.phpuse Symfony\Component\Validator\Constraints as Assert;class Author{    /**     * @Assert\True(message = "The password cannot match your first name")     */    public function isPasswordLegal()    {        // return true or false    }}

XML格式:

<!-- src/Acme/BlogBundle/Resources/config/validation.xml --><class name="Acme\BlogBundle\Entity\Author">    <getter property="passwordLegal">        <constraint name="True">            <option name="message">The password cannot match your first name</option>        </constraint>    </getter></class>

PHP代碼格式:

// src/Acme/BlogBundle/Entity/Author.phpuse Symfony\Component\Validator\Mapping\ClassMetadata;use Symfony\Component\Validator\Constraints\True;class Author{    public static function loadValidatorMetadata(ClassMetadata $metadata)    {        $metadata->addGetterConstraint(‘passwordLegal‘, new True(array(            ‘message‘ => ‘The password cannot match your first name‘,        )));    }}

 

現在我們建立一個isPasswordLegal()方法,並且包含你需要邏輯:

public function isPasswordLegal(){   return ($this->firstName != $this->password);}

眼尖的人可能會注意到getter的首碼("get"或者"is")在映射時被忽略了。這允許你在不改變校正規則的前提下,把一個約束移動到一個具有同名屬性上,反之亦然。


類:
一些約束應用到整個類被校正上面。比如,Callback約束是一個通用約束,它可以應用到類自身。當類被校正時,被約束描述的方法只是被執行這樣每一個可以提供更個人化的校正。


校正分組
到目前為止,你已經能夠添加約束到類並詢問是否該類傳入所有定義的約束規則。一些情況下,你只需要使用該類的其中某些規則來校正一個對象。要做到這些,你可以組織每一個約束到一個或者多個校正組中,然後應用使用其中一組校正。比如,假設你有一個User類,它會在使用者註冊和使用者更新他們的聯絡資訊時使用。
YAML格式:

# src/Acme/BlogBundle/Resources/config/validation.ymlAcme\BlogBundle\Entity\User:    properties:        email:            - Email: { groups: [registration] }        password:            - NotBlank: { groups: [registration] }            - MinLength: { limit: 7, groups: [registration] }        city:            - MinLength: 2

類聲明格式:

// src/Acme/BlogBundle/Entity/User.phpnamespace Acme\BlogBundle\Entity;use Symfony\Component\Security\Core\User\UserInterface;use Symfony\Component\Validator\Constraints as Assert;class User implements UserInterface{    /**    * @Assert\Email(groups={"registration"})    */    private $email;    /**    * @Assert\NotBlank(groups={"registration"})    * @Assert\MinLength(limit=7, groups={"registration"})    */    private $password;    /**    * @Assert\MinLength(2)    */    private $city;}

XML格式:

<!-- src/Acme/BlogBundle/Resources/config/validation.xml --><class name="Acme\BlogBundle\Entity\User">    <property name="email">        <constraint name="Email">            <option name="groups">                <value>registration</value>            </option>        </constraint>    </property>    <property name="password">        <constraint name="NotBlank">            <option name="groups">                <value>registration</value>            </option>        </constraint>        <constraint name="MinLength">            <option name="limit">7</option>            <option name="groups">                <value>registration</value>            </option>        </constraint>    </property>    <property name="city">        <constraint name="MinLength">7</constraint>    </property></class>

PHP代碼格式:

// src/Acme/BlogBundle/Entity/User.phpnamespace Acme\BlogBundle\Entity;use Symfony\Component\Validator\Mapping\ClassMetadata;use Symfony\Component\Validator\Constraints\Email;use Symfony\Component\Validator\Constraints\NotBlank;use Symfony\Component\Validator\Constraints\MinLength;class User{    public static function loadValidatorMetadata(ClassMetadata $metadata)    {        $metadata->addPropertyConstraint(‘email‘, new Email(array(            ‘groups‘ => array(‘registration‘)        )));        $metadata->addPropertyConstraint(‘password‘, new NotBlank(array(            ‘groups‘ => array(‘registration‘)        )));        $metadata->addPropertyConstraint(‘password‘, new MinLength(array(            ‘limit‘  => 7,            ‘groups‘ => array(‘registration‘)        )));        $metadata->addPropertyConstraint(‘city‘, new MinLength(3));    }}

這裡我們配置了兩個校正組:
      default預設組: 包括所有沒有分配到任何組的約束規則
      registration: 只包含了email和password欄位的校正規則

告訴validator使用指定的校正組,傳一個或者多個組名作為validate()方法的第二個參數即可:

$errors = $validator->validate($author,array(‘registration‘));

 


值和數組校正
到目前為止,我們已經看了如何校正整個對象。但是有時候,我們可能想值校正一個單獨的值,比如校正一個字串是不是一個合法的email地址。這非常簡單,在Controller類中進行如下:

// 在controller類前引用相應的校正命名空間use Symfony\Component\Validator\Constraints\Email;public function addEmailAction($email){    $emailConstraint = new Email();    // 所有的校正選項(options)都可以這樣設定    $emailConstraint->message = ‘Invalid email address‘;    // 使用validator來校正一個值    $errorList = $this->get(‘validator‘)->validateValue($email, $emailConstraint);    if (count($errorList) == 0) {        // 這是一個合法的email地址,可以做些什麼    } else {        // 這是一個非法的email地址        $errorMessage = $errorList[0]->getMessage()        // 做一些錯誤處理    }    // ...}

通過調用validator的validateValue方法,你可以傳入一個原始值和一個你要使用的校正對象。該方法會返回一個ConstraintViolationList對象,它扮演的只是一個錯誤資訊數組的角色。集合中的每一個錯誤是一個ConstraintViolation對象,使用對象的getMessage方法可以擷取錯誤資訊。

 

總結:
Symfony2 的validator是一個強大的工具,它可以被用來保證任何對象資料的合法性。它的強大來源於約束規則,你可以把它們應用於你對象的屬性和getter方法。其實,你大多數情況下都是在使用表單時,間接的應用了校正架構,記住它可以被應用於任何地方校正任何對象。

 

參考URL:http://symfony.com/doc/current/book/validation.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.