MVC中的統一驗證機制 續

2021-09-08 02:33:41 字數 3111 閱讀 9852

最近在朋友的建議下,看了另一種驗證方式,事實上就是mvc例項中提供的實體屬性驗證方式,為每個檢視加乙個檢視模型,對檢視模型中的屬性進行特性的

約束即可。具體如下:

web ui可能是這樣

<%using (html.beginform())

%>

model可能是這樣

namespace web.models

[range(18, int.maxvalue, "greaterthan", "age", 18)]

public int age

[range(int.minvalue, 160, "lessthan", "weight", 160)]

public double weight

[required("emailfield", "email")]

[email("emailfield", "email")]

public string email }}

而controller可能是這樣

}最後實體具一類可能是這樣

namespace web.mvc.extensions

#region protected property

protected regex rletters "); } }

///

/// 驗證數字

///

protected regex rdigit "); } }

///

/// 驗證郵編

///

protected regex rpostnumber $"); } }

///

/// 驗證手機

///

protected regex rmobile $"); } }

///

/// 驗證**

///

protected regex rtelephone -\d$"); } }

///

/// 驗證傳真

///

protected regex rfex -\d$"); } }

///

/// 驗證email

///

protected regex remail \.[0-9]\.[0-9]\.)|(([\w-]+\.)+))([a-za-z]|[0-9])(\]?)$"); } }

#endregion

}///

/// 為空驗證

///

[attributeusage(attributetargets.parameter | attributetargets.field | attributetargets.property, allowmultiple = false)]

public class requiredattribute : entityvalidationattribute

public requiredattribute(string messageid, params object args) :

base(messageid, args)

public override bool isvalid(object value)

.isvalid(value);}}

///

/// 範圍驗證

///

[attributeusage(attributetargets.parameter | attributetargets.field | attributetargets.property, allowmultiple = false)]

public class rangeattribute : entityvalidationattribute

public rangeattribute(int minimum, int maximum, string messageid, params object args) :

base(messageid, args)

public rangeattribute(type type, string minimum, string maximum, string messageid, params object args) :

base(messageid, args)

public override bool isvalid(object value)

}///

/// email驗證

///

[attributeusage(attributetargets.parameter | attributetargets.field | attributetargets.property, allowmultiple = false)]

public class emailattribute : entityvalidationattribute

public override bool isvalid(object value)

}///

/// 訊息類

///

public class messagemanager

\"欄位是必填的!");

messages.add("greaterthan", "這個 \"\" 欄位的值必須大於 \"\"!");

messages.add("lessthan", "這個 \"\" 欄位的值必須小於 \"\"!");

messages.add("emailfield", "這個 \"\" 字段不是有效的email位址!");

}///

/// 得到驗證異常的訊息集合

/// 對外公開

///

/// 異常訊息id

/// 訊息引數集合

///

public string getmessage(string messageid, params object args)

///

/// 本類的例項物件

///

public static messagemanager current = new messagemanager();}}

Zend的MVC機制(一)

首先看下zend controller front getinstance是呼叫單例模式,例項化了它的內部屬性 plugins,例項化了乙個zend controller plugin broker類。這個類是管理front的外掛程式的類。先看乙個front中的方法public function r...

Mvc中的資料驗證方法

model 注意其中加了驗證 using system using system.collections.generic using system.componentmodel.dataannotations using system.linq using system.web namespace ...

主題 統一Android應用風格的機制

淺色 holo 主題的 gmail 介面.主題是一種使得 android 應用保持統一風格的機制。主題定義了各種使用者介面所需要的視覺元素,包括顏色 高度 邊界填充和字型大小。為了提公升所有應用的統一性,android 為您在冰淇淋三明治 ice cream sandwich 上設計的應用提供了三種...