Testing required field by saving without providing Postal Code value:
Using Inspect Element, locate the field you want to validate against:
In the code section, create a Graph Extension for SOOrderEntry where you will put your validations:
Write your validation code in that graph extension:
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;
}
}
}
When saving or when the custom postal code field lose focus, validation will be triggered:
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");
}
}
2 回答
添加PXDefault属性应该足以使该字段成为必需 . 如果值为null或为空,PXDefault将阻止保存 . 它会引发错误并突出显示该字段 .
Adding the custom field in SOOrder DAC:
Adding the custom field to Sales Order screen:
Testing required field by saving without providing Postal Code value:
Using Inspect Element, locate the field you want to validate against:
In the code section, create a Graph Extension for SOOrderEntry where you will put your validations:
Write your validation code in that graph extension:
When saving or when the custom postal code field lose focus, validation will be triggered:
您可以通过添加选择器或实现FieldVerifying来检查值 .
如果默认情况下使用PXSelector,则如果在支持表中找不到,则选择器将抛出错误 .
或者,您可以使用字段FieldVerifying事件将其添加到销售订单上的图表扩展中,如下例所示...