概念
定义了一个校验器对象,通常是添加到业务单据操作服务插件,用于对操作的合法性进行校验。继承自校验器抽象对象AbstractValidator。
应用场景
校验器
示例
新建一个类,继承自AbstractValidator。
通常重写方法:
Validate
using Kingdee.BOS; using Kingdee.BOS.Core; using Kingdee.BOS.Core.Bill.PlugIn; using Kingdee.BOS.Core.DynamicForm.PlugIn; using Kingdee.BOS.Core.DynamicForm.PlugIn.Args; using Kingdee.BOS.Core.Metadata; using Kingdee.BOS.Core.Validation; using Kingdee.BOS.Orm.DataEntity; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Kingdee.K3.Example.ServicePlugIn.TestService { /// <summary> /// Test 保存操作服务 /// </summary> public class Save : AbstractOperationServicePlugIn { // // Summary: // 可以定制加载指定字段到实体里 // // Parameters: // e: public override void OnPreparePropertys(BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e) { e.FieldKeys.Add("FMaterialID"); base.OnPreparePropertys(e); } // // Summary: // 通过此事件,通知插件进行添加自定义数据校验器 // // Parameters: // e: public override void OnAddValidators(BOS.Core.DynamicForm.PlugIn.Args.AddValidatorsEventArgs e) { TestValidator validator = new TestValidator(); validator.AlwaysValidate = true; validator.EntityKey = "FBillHead"; e.Validators.Add(validator); } // // Summary: // 调用操作事物前触发 // // Parameters: // e: public virtual void BeginOperationTransaction(BeginOperationTransactionArgs e) { //todo } // // Summary: // 调用操作事物后触发 // // Parameters: // e: public override void EndOperationTransaction(EndOperationTransactionArgs e) { foreach (DynamicObject item in e.DataEntitys) { DynamicObjectCollection entryItems = item["STK_LOTADJUSTENTRY"] as DynamicObjectCollection; //ConvertCommonService.UpdateConvertTo(this.Context, K3.Core.SCM.SCMTableConst.T_STK_LOTADJUSTENTRY, entryItems); } } // Summary: // 执行操作事务后事件,通知插件对象执行其它事务无关的业务逻辑 // // Parameters: // e: public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e) { //todo } // // Summary: // 执行操作事务前事件,通知插件对要处理的数据进行排序等预处理 // // Parameters: // e: // e.SelectedRows 准备要处理的扩展实体 e.DataEntitys 处理后的实体 public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e) { //todo } /// <summary> /// 保存单据头校验器 /// </summary> private class TestValidator : AbstractValidator { public override void Validate(ExtendedDataEntity[] dataEntities, ValidateContext validateContext, Context ctx) { foreach (ExtendedDataEntity obj in dataEntities) { object materialID = obj.DataEntity["MaterialID"]; if (materialID == null) { validateContext.AddError(obj.DataEntity, new ValidationErrorInfo ( "", obj.DataEntity["Id"].ToString(), obj.DataEntityIndex, 0, "001", "错误信息", "标题" )); } } } } } }
K/3 Cloud部门删除校验器
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Kingdee.BOS.Core.Validation; using Kingdee.BOS.Core.DynamicForm.PlugIn; using Kingdee.K3.BD.Common.BusinessEntity; using Kingdee.BOS.Orm.DataEntity; namespace Kingdee.K3.BD.App.Common.ServicePlugIn.Department { /// <summary> /// 部门 删除操作 服务端插件 /// </summary> public class Delete : AbstractOperationServicePlugIn { public override void OnPreparePropertys(BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e) { base.OnPreparePropertys(e); e.FieldKeys.Add("FName"); e.FieldKeys.Add("FIsHRDept"); e.FieldKeys.Add("FDocumentStatus"); e.FieldKeys.Add("FNumber"); } public override void OnAddValidators(BOS.Core.DynamicForm.PlugIn.Args.AddValidatorsEventArgs e) { base.OnAddValidators(e); AbstractValidator validator; validator = new DeleteValidator(); validator.EntityKey = "FBillHead"; e.Validators.Add(validator); } } /// <summary> /// 部门删除校验器 /// </summary> internal class DeleteValidator : AbstractValidator { public override void Validate(BOS.Core.ExtendedDataEntity[] dataEntities, ValidateContext validateContext, BOS.Context ctx) { //获取所有部门的层级码信息 IList<LevelCodeTreeNode> nodeList = Kingdee.K3.BD.Contracts.ServiceFactory.GetDepartmentService(Context).InitialLevelCodeTreeNodes(Context, (from dynObj in dataEntities where dynObj.DataEntity["DocumentStatus"].ToString() == "A" select Convert.ToInt64(dynObj.DataEntity["Id"])).ToList()); for (int i = 0; i < nodeList.Count; i++) { LevelCodeTreeNode currentNode = nodeList[i]; if (currentNode.ChildNodes == null || currentNode.ChildNodes.Count == 0) { RemoveNode(nodeList, currentNode); } } //获取非删除部门列表 IList<LevelCodeTreeNode> unDelNodes = (from item in nodeList where item.ChildNodes != null && item.ChildNodes.Count != 0 select item).ToList(); foreach (var unDelNode in unDelNodes) { BOS.Core.ExtendedDataEntity dynObj = FindDataObject(dataEntities, unDelNode); validateContext.AddError(dynObj.DataEntity, new ValidationErrorInfo( dynObj.DataEntity["Number"].ToString(), dynObj.DataEntity["Id"].ToString(), dynObj.DataEntityIndex, dynObj.RowIndex, dynObj.DataEntity["Id"].ToString(), string.Format(Kingdee.BOS.Resource.ResManager.LoadKDString("存在下级部门,建议先删除子部门后再删除本部门!","001001000000351",Kingdee.BOS.Resource.SubSystemType.BASE)), dynObj.DataEntity["Number"].ToString() )); } } /// <summary> /// 获取当前树节点对应的实体数据 /// </summary> /// <param name="dataEntities"></param> /// <param name="currentNode"></param> /// <returns></returns> private BOS.Core.ExtendedDataEntity FindDataObject(BOS.Core.ExtendedDataEntity[] dataEntities, LevelCodeTreeNode currentNode) { foreach (var dynObj in dataEntities) { if (Convert.ToInt64(dynObj.DataEntity["Id"]) == currentNode.Id) { return dynObj; } } //原则上不会到这里 return new BOS.Core.ExtendedDataEntity(); } /// <summary> /// 清楚已被确认删除的树节点 /// </summary> /// <param name="nodeList"></param> /// <param name="removeNode"></param> private void RemoveNode(IList<LevelCodeTreeNode> nodeList, LevelCodeTreeNode removeNode) { for (int i = 0; i < nodeList.Count; i++) { LevelCodeTreeNode checkNode = nodeList[i]; for (int j = checkNode.ChildNodes.Count - 1; j >= 0; j--) { LevelCodeTreeNode childNode = checkNode.ChildNodes[j]; if (childNode.Id == removeNode.Id) { checkNode.ChildNodes.RemoveAt(j); } } } } } }