// 场景:表单保存校验(客户非空、分录数量>0) // 适用:AbstractBillPlugIn 界面保存 // 依赖 API:BeforeDoOperation、Model.GetValue、View.ShowErrMessage // 相关:enterprise-plugin-shape.md · FormPluginTemplate.cs // // 必须: // - 事件 BeforeDoOperation(OperateKey=Save)或 BeforeSave // 禁止: // - BeforeExecuteOperationTransaction(操作事务,属操作插件) // - OnValidate 等未在 Form 手册中作为保存主路径的事件名 using System; using System.ComponentModel; using Kingdee.BOS.Core.Bill.PlugIn; using Kingdee.BOS.Core.DynamicForm.PlugIn.Args; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.Util; namespace MyCompany.MyProject.BillPlugIn { [Description("保存校验样例"), HotUpdate] public class SaveValidateSample : AbstractBillPlugIn { // Key(GetValue/SetValue)— 以元数据为准 private const string KEY_CUST = "FCustId"; private const string KEY_QTY = "FQty"; private const string KEY_ENTRY = "FSaleOrderEntry"; public override void BeforeDoOperation(BeforeDoOperationEventArgs e) { base.BeforeDoOperation(e); if (!e.OperateKey.EqualsIgnoreCase("Save")) return; object cust = this.Model.GetValue(KEY_CUST); if (cust == null) { e.Cancel = true; this.View.ShowErrMessage("保存失败:客户不能为空。"); return; } int rowCount = this.Model.GetEntryRowCount(KEY_ENTRY); if (rowCount <= 0) { e.Cancel = true; this.View.ShowErrMessage("保存失败:分录至少一行。"); return; } for (int i = 0; i < rowCount; i++) { object qtyObj = this.Model.GetValue(KEY_QTY, i); decimal qty = qtyObj != null ? Convert.ToDecimal(qtyObj) : 0m; if (qty <= 0m) { e.Cancel = true; this.View.ShowErrMessage($"保存失败:第 {i + 1} 行数量必须大于 0。"); return; } } } } }