【问题标题】:Acumatica : Trigger Save validation on another Custom ButtonAcumatica:在另一个自定义按钮上触发保存验证
【发布时间】:2019-07-16 23:35:55
【问题描述】:

美好的一天!

单击自定义按钮时如何触发视图验证?比如我在 DAC 中有一个必填字段 Amount 并且它有 PXDefault 属性,如果我保存记录而不填写它,它自然会触发 “错误:'Amount'不能为空” 错误。现在从这里开始,我想在触发 SAVE 按钮之外的另一个按钮时复制此行为。

您如何触发该验证?我已经尝试在字段本身上添加 PXUIVerify 属性,但它已经在页面加载期间触发,我尝试通过添加 Attribute 属性 CheckOnRowSelected = false 来禁用它,但无济于事,它仍然会触发验证。

感谢任何建议和答案。非常感谢。

其他问题:

验证表单的正确方法是什么?

【问题讨论】:

    标签: acumatica


    【解决方案1】:

    字段是否为空的验证发生在 PXDefaultAttribute 中的 RowPersisting 事件中。屏幕上是否有任何错误的验证发生在 PXUIFieldAttribute 中的 CommandPreparing 事件中。

    由于您实际上并没有保存,因此复制验证而不是尝试“触发”它会更容易。以下代码 sn-p 尝试通过为图表上的每个视图验证缓存中的每条记录来复制验证。

    public static void Validate(PXGraph graph)
    {
        for (int k = 0; k < graph.Views.Caches.Count; ++k)
        {
            PXCache cache = graph.Caches[graph.Views.Caches[k]];
            PXEntryStatus status;
    
            foreach (object rec in cache.Cached)
            {
                status = cache.GetStatus(rec);
    
                if (cache.GetStatus(rec) == PXEntryStatus.Updated || cache.GetStatus(rec) == PXEntryStatus.Inserted)
                {
                    cache.Current = rec;
    
                    foreach (PXDefaultAttribute attribute in cache.GetAttributesReadonly(rec, null).OfType<PXDefaultAttribute>())
                    {
                        CheckDefaultAttribute(attribute.PersistingCheck, attribute.FieldName, rec, cache);
                    }
    
                    foreach (PXDBDefaultAttribute attribute in cache.GetAttributesReadonly(rec, null).OfType<PXDBDefaultAttribute>())
                    {
                        CheckDefaultAttribute(attribute.PersistingCheck, attribute.FieldName, rec, cache);
                    }
    
                    // Verifies that there are no errors on the page.
                    foreach (IPXInterfaceField field in cache.GetAttributesReadonly(rec, null).OfType<IPXInterfaceField>())
                    {
                        if (!string.IsNullOrEmpty(field.ErrorText) && (field.ErrorLevel == PXErrorLevel.Error || field.ErrorLevel == PXErrorLevel.RowError))
                        {
                            throw new PXException(field.ErrorText);
                        }
                    }
                }
            }
        }
    }
    
    // Verifies that the field has a value if the PersistingCheck is not PXPersistingCheck.Nothing
    protected static void CheckDefaultAttribute(PXPersistingCheck persistingCheck, string fieldName, object row, PXCache cache)
    {
        if (persistingCheck != PXPersistingCheck.Nothing)
        {
            object value = cache.GetValue(row, fieldName);
    
            if (value == null || (persistingCheck == PXPersistingCheck.NullOrBlank && value is string && ((string)value).Trim() == string.Empty))
            {
                throw new PXException(ErrorMessages.FieldIsEmpty, PXUIFieldAttribute.GetDisplayName(cache, fieldName));
            }
        }
    }
    

    作为免责声明,此方法将错过自定义 RowPersisting 事件中发生的任何验证。

    【讨论】:

      【解决方案2】:

      您可以调用 RaiseFieldVerifying 事件。检查 ARDocumentEnq 图,CreateInvoice(输入新发票)按钮调用 FieldVerifying 和 FieldUpdated 事件,触发这些事件。您可以捕获异常,例如在 INTransferEntry 图中:

      protected virtual void INRegister_TransferType_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
      {
          INRegister row = (INRegister)e.Row;
          {
              object toSiteID = row.ToSiteID;
              try
              {
                  sender.RaiseFieldVerifying<INRegister.toSiteID>(row, ref toSiteID);
                  sender.RaiseExceptionHandling<INRegister.toSiteID>(row, toSiteID, null);
              }
              catch (PXSetPropertyException ex)
              {
                  sender.RaiseExceptionHandling<INRegister.toSiteID>(row, toSiteID, new PXSetPropertyException(ex, PXErrorLevel.Error, Messages.WarehouseNotAllowed, Messages.OneStep));
              }
          }
      }
      

      【讨论】:

      • 你好@KRichardson,有什么动态的方法吗?我的意思是,有没有办法让我触发具有 PXDefault 属性的字段的验证?例如,我有 10 个必填字段,在保存记录之前我需要单击一个流程按钮,现在我想引发这些必填字段的错误,有没有办法让我动态触发这些字段?不做 RaiseExceptionHandling 方法?
      • Save.Press() 按钮将触发所有必填字段验证,但您想在保存前进行验证。过去,我在需要验证的图表上的流程上编写自定义逻辑,这与输入数据不同,例如“验证”整个文档,并将它们直接烘焙到按钮的执行中。这可能是在尝试之前处理所需值的最佳方式
      • 我希望有可以调用的方法来验证整个缓存。但无论如何,感谢您的建议和回答,我会想办法验证整个图形视图。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 2010-11-02
      • 2020-08-01
      相关资源
      最近更新 更多