vici mvc開發第七篇——Form Validation

一、Form Validation

vici mvc包括 HTML 窗體的一種非常強大和靈活的驗證機制。在上一節中所示,我們知道可以通過三種不同的機制來驗證窗體。您可以任意組合所有這些機制來驗證一個單個窗體。

可以按如下方法驗證:

1、驗證字段的屬性

2、特定字段的字段驗證方法

3、全局驗證方法

二、 Validation using attributes

在每一個WebForm的繼承類裏,你可以定義一些特殊的驗證屬性,如下:

  • [ValidateRequired]
  • [ValidateLength(minLength)]
  • [ValidateLength(minLength,maxLength)]
  • [ValidateRegex(regex)]
  • [ValidateRange(min,max)]
  • [ValidateExpression(expr)]

大多數的這些驗證屬性不需要進一步解釋,除了爲ValidateExpression 屬性,見下面的解釋:

對於驗證屬性最重要的一點就是當某個字段的值爲空的時候驗證成功,例如,當用戶什麼也不填寫時您指定的屬性 [ValidateLength3]將會驗證成功,如果有一個字符填進去就將失敗。 要防止出現這種情況,應增加 [ValidateRequired] 屬性。

1)The ValidateExpression attribute

ValidateExpression 屬性是 基於自定義驗證表達式的 一個非常強大的驗證器,它允許您指定表達式的計算結果爲 真或者假的實際意義驗證成功。 它允許您指定一個表達式,而它的計算結果可以爲真或者假。這個表達式可以接受任何c#表達式返回的bool值,同時支持 strings, numbers, booleans等。此外,您可以引用其他窗體的字段,只需通過在表達式中使用字段名稱。特殊的可用的關鍵字的表示該字段在哪定義的。

三、 Validation using the field validation method

在 WebForm的繼承類中,你可以重寫 ValidateField()方法,它有三個參數,如果驗證失敗,那麼返回值就應該是假,如果返回值爲真,那麼錯誤信息將會被忽略。

可以按下面方法定義:

四、Validation error messages and CSS classe

當窗體驗證失敗,vici mvc將會呈現不同CSS類的無效字段,至於如何指定CSS類在上一節中已經做了介紹。

此外, ViewData將會自動填充倆個值:

  • ValidationResult
  • ValidationErrors

ValidationResult 變量是一個 包含窗體驗證結果的信息的 對象,他會在呈現錯誤信息時顯示給用戶。 ValidationResult對象含有如下屬性和方法:

Success true if there are no validation errors, false if there is at least one validation error.
Messages A list of validation error messages. This list does not contain the error messages themselves, but objects with two properties "Message" and "ControlNames". "Message" contains the actual validation message, while "ControlNames" is a list of control names associated with this validation error message. This usually contains only one name, but you could have several controls that fail validation but generate the same error message. It would be pointless to show the same message twice, so Vici MVC solves this by returning only one error message, but linking it to more than one control (name)
Controls A list of objects, one for each control that failed validation. The objects have two properties: "ControlName" and "Messages". "ControlName" is the name of the control, while "Messages" is a list of validation error messages associated with this control.
MessagesForControl(controlName) returns a list of all validation error messages for a specific control (name)
ControlsForMessage(message) returns a list of all control names for a specific validation message
HasErrors(controlName) returns true if the given control has any errors associated with it
HasMessages(controlName) returns true if the given control has any validation messages associated with it

請注意,已驗證的沒有相互關聯控件的錯誤信息和相互關聯的驗證信息不完全正確的無效的控件。錯誤信息框架會自動翻譯,所以錯誤的驗證信息可以包含在標籤裏:

請注意“ ValidationResult.Messages”的寫法,因爲空的錯誤鏈表(list)將會被認定爲假,所以這是最好的寫法。

如果我們想要在每個字段旁邊放置一個有錯誤信心的標記,該這麼辦了?如下方法即可:

五、 Automatic client side (Ajax) validation

vici mvc支持驗證驗證框體時使用ajax,這將得到更好的用戶體驗因爲當驗證失敗時不需要重新加載窗體。

你必須做的就是在窗體類上添加[AjaxValidated]屬性,vici mvc將會自動創建一個javascript 方法$Mvc.ValidateForm(),它接受一個參數 對包含您要驗證的輸入的字段的 HTML 窗體元素的引用。它的返回值是一個 "ValidationResult"類型的對象,唯一的限制是它只包含屬性沒有方法。

  • Success
  • Messages
  • Controls

如何使用AJAX驗證可以去查看官方網站的案例教材。

六、 Creating custom validation attributes

當然,你也可以建立同樣可以添加到FORM的屬於自己的驗證屬性。創建一個從 ValidationAttribute派生的類,並且重新其中的 Validate()方法即可:

protected bool Validate(object value, Type fieldType)

fieldType參數是一個字段相關聯類型,但是大多數情況下不需要。value是一個你想要驗證的值。下面是一個 ValidateLength屬性的 validate 方法:

很重要的一點就是當字段爲空 Validate()方法返回真。Exmplained 作爲較早版本, 除非 [ValidateRequired] 屬性添加到字段, 空字段始終有效。

未完待續:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章