我在“销售订单”屏幕中创建了一个新的自定义字段(邮政编码 - Usrpostalcode),我正在尝试使该字段成为必需(即使在添加[PXDefault]后仍无效) [PXUIField(...,Required = true)]),验证它并确保它与“运输设置”中的邮政编码匹配。
任何人都可以帮我吗?
在销售订单屏幕上创建货件时出现此错误
答案 0 :(得分:0)
您可以通过添加选择器或实现FieldVerifying来检查值。
如果默认情况下使用PXSelector,则如果在支持表中找不到,则选择器将抛出错误。
或者,您可以使用字段FieldVerifying事件将其添加到销售订单上的图表扩展名中,如下例所示...
public class SOOrderEntryExtension : PXGraphExtension<SOOrderEntry>
{
protected virtual void SOOrder_Usrpostalcode_FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
{
//search for table value...
// if not found...
throw new PXSetPropertyException<SOOrder.usrpostalcode>("Invalid postal code");
}
}
答案 1 :(得分:0)
添加PXDefault属性应足以使该字段成为必需字段。如果值为null或为空,PXDefault将阻止保存。它会引发错误并突出显示该字段。
在代码部分,为SOOrderEntry创建图表扩展,您可以在其中放置验证:
在图表扩展中写下您的验证码:
namespace PX.Objects.SO
{
public class SOOrderEntry_Extension:PXGraphExtension<SOOrderEntry>
{
public const string postalCodeErrorMessage = "Sales Order postal code must match shipping address postal code.";
// Validate just before saving, triggered when graph save function is called
public void SOOrder_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
{
if (!ValidatePostalCode(sender, e.Row as SOOrder))
{
// Raise field error
PXUIFieldAttribute.SetError<SOOrderExt.usrPostalCode>(sender, e.Row, postalCodeErrorMessage);
}
}
// Validation function
public bool ValidatePostalCode(PXCache sender, SOOrder soOrder)
{
if (soOrder != null)
{
// Get SOOrder custom field Postal Code
SOOrderExt soOrderExt = sender.GetExtension<SOOrderExt>(soOrder);
if (soOrderExt != null)
{
string soPostalCode = soOrderExt.UsrPostalCode;
// Get current shipping address displayed on Sales Order
SOShippingAddress shippingAddress = Base.Shipping_Address.Current as SOShippingAddress;
if (shippingAddress != null)
{
// Special case to handle null values
if (soPostalCode == null || shippingAddress.PostalCode == null)
{
return soPostalCode == shippingAddress.PostalCode;
}
// Compare postal codes
soPostalCode =soPostalCode.Trim().Replace(" ", "");
string shippingPostalCode = shippingAddress.PostalCode.Trim().Replace(" ", "");
return soPostalCode.Equals(shippingPostalCode, StringComparison.OrdinalIgnoreCase);
}
}
}
return false;
}
}
}