52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using System;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Zhaizj.Framework.ORM {
|
|
|
|
/// <summary>
|
|
/// 正则表达式批注,验证属性的对象是否符合指定的正则表达式
|
|
/// </summary>
|
|
[Serializable]
|
|
public class PatternAttribute : ValidationAttribute {
|
|
|
|
private String _regexp;
|
|
|
|
public PatternAttribute() {
|
|
}
|
|
|
|
public PatternAttribute( String regexp ) {
|
|
_regexp = regexp;
|
|
}
|
|
|
|
public PatternAttribute( String regexp, String msg ) {
|
|
_regexp = regexp;
|
|
base.Message = msg;
|
|
}
|
|
|
|
public String Regexp {
|
|
get { return _regexp; }
|
|
set { _regexp = value; }
|
|
}
|
|
|
|
public override void Validate( String action, IEntity target, EntityPropertyInfo info, Result result ) {
|
|
|
|
if (!Regex.IsMatch( cvt.ToNotNull( target.get( info.Name ) ), this.Regexp, RegexOptions.Singleline )) {
|
|
if (strUtil.HasText( this.Message )) {
|
|
result.Add( this.Message );
|
|
}
|
|
|
|
else {
|
|
EntityInfo ei = Entity.GetInfo( target );
|
|
String str = "[" + ei.FullName + "] : property \"" + info.Name + "\" ";
|
|
result.Add( str + " is not match the format pattern : " + this.Regexp );
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|