昨天给大家介绍了实体FluentValidation验证,今天继续给大家介绍表单验证,在Rookey.Frame框架中,表单验证有PrimaryKeyFields字段验证、唯一验证、必填验证、常用验证及自定义验证,PrimaryKeyFields字段验证昨天也略微介绍了下PrimaryKeyFields对记录唯一性进行验证,也即是由定义的一个或多个字段唯一确定一条记录,保存时不允许重复,根据定义的,就是在实体表定义时在[ModuleConfig]实体类属性标记中定义,也可以在模块管理界面修改
针对唯一性验证,可以在实体类属性字段标记[FieldConfig(IsUnique = true, IsRequired = true)]中定义,也可以在表单管理中修改
必填验证同理。
针对常用验证功能只能在表单中配置,打开表单管理在表单字段中修改验证类型:
目前支持的常用验证类型见如下枚举定义:
/// <summary> /// 验证类型 /// </summary> public enum ValidateTypeEnum { /// <summary> /// 无 /// </summary> [Description("无")] No = 0, /// <summary> /// Eamil /// </summary> [Description("邮箱")] email = 1, /// <summary> /// URL /// </summary> [Description("网址")] url = 2, /// <summary> /// 整型 /// </summary> [Description("整型")] intNum = 3, /// <summary> /// 浮点型 /// </summary> [Description("浮点型")] floatNum = 4, /// <summary> /// 手机号码 /// </summary> [Description("手机")] mobile = 5, /// <summary> /// QQ /// </summary> [Description("QQ")] qq = 6, /// <summary> /// 邮政编码 /// </summary> [Description("邮政编码")] zip = 7, /// <summary> /// 电话号码 /// </summary> [Description("电话")] phone = 8, /// <summary> /// 传真 /// </summary> [Description("传真")] faxno = 9, /// <summary> /// 身份证 /// </summary> [Description("身份证")] idCard = 10 }
针对自身项目需要也可以将其他验证添加到其中
另外表单验证中针对数值型还可以设置最大最小值验证,针对字段串字段可以设置最大最小字符长度验证,同样在在表单字段中设置
表单验证代码如下:
/// <summary> /// 表单字段配置验证 /// </summary> /// <param name="moduleId">模块Id</param> /// <param name="model">实体对象</param> /// <param name="verifyFields">需要验证字段的字段集合</param> /// <returns></returns> public static string FormFieldVerify(Guid moduleId, object model, List<string> verifyFields) { Type modelType = GetModelType(moduleId); PropertyInfo idProperty = modelType.GetProperty("Id"); Guid id = idProperty.GetValue(model, null).ObjToGuid(); //当前记录Id //表单字段验证 if (verifyFields != null && verifyFields.Count > 0) { foreach (string field in verifyFields) { PropertyInfo p = modelType.GetProperty(field); if (p == null) continue; object value = p.GetValue(model, null); Sys_FormField formField = SystemOperate.GetDefaultFormSingleField(moduleId, field); if (formField == null) continue; string fieldDisplay = SystemOperate.GetFieldDisplay(moduleId, field); #region 必填验证 if (formField.IsRequired.HasValue && formField.IsRequired.Value) //必填验证 { ControlTypeEnum crlType = formField.ControlTypeOfEnum; string requiredDes = crlType == ControlTypeEnum.ComboBox || crlType == ControlTypeEnum.ComboGrid || crlType == ControlTypeEnum.ComboTree ? "请选择" : "不能为空"; bool isForeignField = SystemOperate.IsForeignField(moduleId, field); //是否是外键字段 if (value == null || (isForeignField && value.ObjToGuid() == Guid.Empty)) { return string.Format("【{0}】为必填字段,{1}!", fieldDisplay, requiredDes); } } if (formField.ControlTypeOfEnum == ControlTypeEnum.IntegerBox || formField.ControlTypeOfEnum == ControlTypeEnum.NumberBox) //最大值、最小值验证 { if (formField.MinValue.HasValue && value.ObjToDecimal() < formField.MinValue.Value || formField.MaxValue.HasValue && value.ObjToDecimal() > formField.MaxValue.Value) { return string.Format("【{0}】的值介于【{1}】~【{2}】之间!", fieldDisplay, formField.MinValue.Value, formField.MaxValue.Value); } } if (p.PropertyType == typeof(String) && ((formField.MinCharLen.HasValue && formField.MinCharLen.Value > 0) || (formField.MaxCharLen.HasValue && formField.MaxCharLen.Value > 0))) //字符长度验证 { if (value.ObjToStr().Length < formField.MinCharLen.Value || value.ObjToStr().Length > formField.MaxCharLen.Value) { return string.Format("【{0}】字符长度在【{1}】~【{2}】之间!", fieldDisplay, formField.MinCharLen.Value, formField.MaxCharLen.Value); } } #endregion #region 基本验证 if (formField.ValidateTypeOfEnum != ValidateTypeEnum.No && p.PropertyType == typeof(String)) { //基本验证 switch (formField.ValidateTypeOfEnum) { case ValidateTypeEnum.email: { bool rs = Validator.IsEmail(value.ObjToStr()); if (!rs) return string.Format("【{0}】字段值无效,请输入正确的邮箱地址!", fieldDisplay); } break; case ValidateTypeEnum.idCard: //身份证验证 { bool rs = Validator.IsIDCard(value.ObjToStr()); if (!rs) return string.Format("【{0}】字段值无效,请输入正确的身份证号码!", fieldDisplay); } break; case ValidateTypeEnum.intNum: { bool rs = Validator.IsInteger(value.ObjToStr()); if (!rs) return string.Format("【{0}】字段值无效,只能输入整数值!", fieldDisplay); } break; case ValidateTypeEnum.floatNum: { bool rs = Validator.IsNumber(value.ObjToStr()); if (!rs) return string.Format("【{0}】字段值无效,只能输入数值!", fieldDisplay); } break; case ValidateTypeEnum.faxno: case ValidateTypeEnum.phone: { bool rs = Validator.IsTelePhoneNumber(value.ObjToStr()); if (!rs) return string.Format("【{0}】字段值无效,请输入正确的固定电话号码!", fieldDisplay); } break; case ValidateTypeEnum.mobile: { bool rs = Validator.IsMobilePhoneNumber(value.ObjToStr()); if (!rs) return string.Format("【{0}】字段值无效,请输入正确的手机号码!", fieldDisplay); } break; case ValidateTypeEnum.qq: { bool rs = Validator.IsIntegerPositive(value.ObjToStr()); if (!rs) return string.Format("【{0}】字段值无效,请输入正确的QQ号码!", fieldDisplay); } break; case ValidateTypeEnum.url: { bool rs = Validator.IsURL(value.ObjToStr()); if (!rs) return string.Format("【{0}】字段值无效,请输入正确的URL!", fieldDisplay); } break; case ValidateTypeEnum.zip: { bool rs = Validator.IsZipCode(value.ObjToStr()); if (!rs) return string.Format("【{0}】字段值无效,请输入正确的邮编!", fieldDisplay); } break; } } #endregion #region 字段唯一性验证 if (formField.IsUnique.HasValue && formField.IsUnique.Value) //唯一性验证 { List<ConditionItem> conditonItems = new List<ConditionItem>() { new ConditionItem() { Field = field, Method = QueryMethod.Equal, Value = value } }; if (id != Guid.Empty) //编辑时排除当前记录 { conditonItems.Add(new ConditionItem() { Field = "Id", Method = QueryMethod.NotEqual, Value = id }); } object exp = GetQueryCondition(moduleId, conditonItems); string errMsg = string.Empty; object tempModel = GetEntity(moduleId, exp, null, out errMsg); if (tempModel != null) { return string.Format("【{0}】=【{1}】的记录已存在,请不要重复添加!", fieldDisplay, SystemOperate.GetFieldDisplayValue(moduleId, model, formField)); } } #endregion } } return string.Empty; }
针对自定义验证,框架提供了验证接口
/// <summary> /// 操作处理接口 /// </summary> /// <typeparam name="T"></typeparam> public interface IModelOperateHandle<T> where T : class {/// <summary> /// 单个实体操作前验证或处理,针对新增保存前、删除前、修改保存前 /// </summary> /// <param name="operateType">操作类型</param> /// <param name="t">操作对象</param> /// <param name="errMsg">错误信息</param> /// <param name="otherParams">其他参数</param> /// <returns>是否通过验证</returns> bool BeforeOperateVerifyOrHandle(ModelRecordOperateType operateType, T t, out string errMsg, object[] otherParams = null);
/// <summary> /// 多个实体操作前验证或处理,针对新增保存前、删除前、修改保存前 /// </summary> /// <param name="operateType">操作类型</param> /// <param name="ts">操作对象集合</param> /// <param name="errMsg">错误信息</param> /// <param name="otherParams">其他参数</param> /// <returns>是否通过验证</returns> bool BeforeOperateVerifyOrHandles(ModelRecordOperateType operateType, List<T> ts, out string errMsg, object[] otherParams = null); #endregion }
例如针对工作流程删除验证:
class Bpm_WorkFlowOperateHandle : IModelOperateHandle<Bpm_WorkFlow> {/// <summary> /// 操作前验证,如果流程已经在运行则不能删除 /// </summary> /// <param name="operateType"></param> /// <param name="t"></param> /// <param name="errMsg"></param> /// <param name="otherParams"></param> /// <returns></returns> public bool BeforeOperateVerifyOrHandle(ModelRecordOperateType operateType, Bpm_WorkFlow t, out string errMsg, object[] otherParams = null) { errMsg = string.Empty; if (operateType == ModelRecordOperateType.Del) { //如果该流程存在流程实例则不允许删除 Bpm_WorkFlowInstance workflowInst = CommonOperate.GetEntity<Bpm_WorkFlowInstance>(x => x.Bpm_WorkFlowId == t.Id, null, out errMsg); if (workflowInst != null) { errMsg = "运行中的流程不允许删除!"; return false; } } return true; } /// <summary> /// 操作前验证 /// </summary> /// <param name="operateType"></param> /// <param name="ts"></param> /// <param name="errMsg"></param> /// <param name="otherParams"></param> /// <returns></returns> public bool BeforeOperateVerifyOrHandles(ModelRecordOperateType operateType, List<Bpm_WorkFlow> ts, out string errMsg, object[] otherParams = null) { errMsg = string.Empty; if (operateType == ModelRecordOperateType.Del) { foreach (Bpm_WorkFlow t in ts) { //如果该流程存在流程实例则不允许删除 Bpm_WorkFlowInstance workflowInst = CommonOperate.GetEntity<Bpm_WorkFlowInstance>(x => x.Bpm_WorkFlowId == t.Id, null, out errMsg); if (workflowInst != null) { errMsg += string.Format("【{0}】,", t.Name); } } if (errMsg.Length > 0) { errMsg = string.Format("流程{0}正在运行不允许删除!", errMsg); return false; } } return true; } }
OK,由于时间有限今天Rookey.Frame框架验证功能就介绍到此地,祝大家生活愉快!