Question

PXUIFieldAttribute.SetError doesn't work on selector field


Userlevel 3
Badge

Hello colleagues,

I am implementing event handler for StockItem to force User must enter value for selector Weight UOM. The field Weight UOM doesn’t have red red icon on the left of label

using the following code:

protected void InventoryItem_RowPersisting(PXCache cache, PXRowPersistingEventArgs e)
{
var row = (InventoryItem)e.Row;
if (row == null) return;
if (e.Operation == PXDBOperation.Insert)
{
if (string.IsNullOrEmpty(row.WeightUOM))
{
var fieldDisplayName = PXUIFieldAttribute.GetDisplayName<InventoryItem.weightUOM>(cache);
var localizedErrorMessage = PXMessages.LocalizeFormat("Field {0} can't be empty", fieldDisplayName);
PXUIFieldAttribute.SetError<InventoryItem.weightUOM>(cache, row, null);
cache.RaiseExceptionHandling<InventoryItem.weightUOM>(e.Row, row.WeightUOM, new PXSetPropertyException(localizedErrorMessage, PXErrorLevel.Error));
}
}
}

I used the same code for field Weight (type: decimal?) and it works fine

Do you have any idea?


15 replies

Userlevel 7
Badge +11

Hi @mrthanhkhoi 

You can add the Required attribute to display the red asterisk symbol. However, the error message is not appearing for the specific field.
 

  The below code is for the extension of the weight UOM field.

   [INUnit(null, typeof(CommonSetup.weightUOM), DisplayName = "Weight UOM",),Required =true]
    protected virtual void InventoryItem_WeightUOM_CacheAttached(PXCache cache)
    {
    
    }

 

Userlevel 7
Badge +8

@mrthanhkhoi you will need to know there are two pieces to this. 1) making the field mandatory when persisting and 2) visual display.
What drives the persisting requirement is PersistingCheck of PXDefault (if set to nothing won’t check, nullorblank used for string check and null for non-string types). 

what drives the presentation (*) is Required to true in PXUIField or predefined types.

Userlevel 3
Badge

Hi @aaghaei, I tried adding persistingcheck for field VolumeUOM by this code

#region WeightUOM
[PXMergeAttributes(Method = MergeMethod.Merge)]

[PXCustomizeBaseAttribute(typeof(INUnitAttribute), "PersistingCheck", PXPersistingCheck.NullOrBlank)]
public string WeightUOM{ get; set; }

However, I am still not able to set error message for this field which is most important requirement.

The default config of this field:

Do you have any idea?

Userlevel 7
Badge +8

In your graph extension on the WeightUOM CacheAttached event properties set:

PXNergeMethod Method to Merge,

PXDefault PersistingCheck to null and

PXUIField Required to true

if could not make it work share this piece of code so that we can have a look 

Userlevel 7
Badge +11

Hi @aaghaei,

Even though we assign the properties to the field, the validation is not being displayed for that specific field. Instead, an error message is thrown upon saving."

Userlevel 7
Badge +8

Please share your Graph Ext for CacheAttached

Userlevel 7
Badge +11

Hi @aaghaei 

Please refer below,

namespace PX.Objects.IN
{
            [PXNonInstantiatedExtension]
    public class IN_InventoryItem_ExistingColumn : PXCacheExtension<PX.Objects.IN.InventoryItem>
    {
        #region WeightUOM    
        [PXMergeAttributes(Method = MergeMethod.Append)]
        [PXCustomizeBaseAttribute(typeof(INUnitAttribute), "Required", true)]
        [PXCustomizeBaseAttribute(typeof(INUnitAttribute), "PersistingCheck", PXPersistingCheck.NullOrBlank)]
            public string WeightUOM { get; set; }
        #endregion
    }
}
 

 

Userlevel 7
Badge +8

This is the DAC Ext. Please share your Graph CacheAttached Ext.

Userlevel 7
Badge +11

Hi @aaghaei ,
I’ve just tried from the customization project screen by using the Customize attribute and the Override on screen level.

The below sample code for overriding on screen level,

namespace PX.Objects.IN
{
    public class InventoryItemMaint_Extension : PXGraphExtension<InventoryItemMaint>
    {
        #region Event Handlers        
        [INUnit(null, typeof(CommonSetup.weightUOM), DisplayName = "Weight UOM",Required =true,PersistingCheck =PXPersistingCheck.NullOrBlank)]
        protected virtual void InventoryItem_WeightUOM_CacheAttached(PXCache cache)
        {

        }
        #endregion
    }

 

 ​

Userlevel 3
Badge

hello @aaghaei, could you please take a look? I can’t find the solutions yet

Userlevel 7
Badge +11

Hi @mrthanhkhoi 

I would suggest creating an Acumatica support ticket for this issue, and once it is resolved, please post the resolution here.

Userlevel 5
Badge +1

@mrthanhkhoi 

From your code, it appears you're attempting to perform validation on the WeightUOM field during the RowPersisting event, which occurs just before the row is saved to the database.

However, the RowPersisting event may not be the best place for this kind of validation, because the UI might not refresh as expected to reflect the error. Instead, consider using the FieldVerifying or RowSelected events for your validation.

However, if you still want to use RowPersisting, there are two issues that I'd like to point out in your code:

  1. The SetError method: You are setting the error message to null. It should be the actual error message.

  2. The RaiseExceptionHandling method: The second argument should be the value that causes the error. But since you are checking for an empty or null value, you can leave it null or empty.

Here's your corrected code:

 

protected void InventoryItem_RowPersisting(PXCache cache, PXRowPersistingEventArgs e)
{
var row = (InventoryItem)e.Row;
if (row == null) return;

if (e.Operation == PXDBOperation.Insert || e.Operation == PXDBOperation.Update)
{
if (string.IsNullOrEmpty(row.WeightUOM))
{
var fieldDisplayName = PXUIFieldAttribute.GetDisplayName<InventoryItem.weightUOM>(cache);
var localizedErrorMessage = PXMessages.LocalizeFormatNoPrefix("Field {0} can't be empty", fieldDisplayName);
PXUIFieldAttribute.SetError<InventoryItem.weightUOM>(cache, row, localizedErrorMessage);
throw new PXRowPersistingException(typeof(InventoryItem.weightUOM).Name, null, localizedErrorMessage);
}
}
}

I've also included e.Operation == PXDBOperation.Update in the conditional check to make sure the validation also applies when the record is being updated, not just inserted.

Finally, the RaiseExceptionHandling method was replaced with PXRowPersistingException. This exception will interrupt the persisting operation, preventing the system from saving the record to the database.

Userlevel 3
Badge

Hello @davidnavasardyan09,

First of all, thank you for your answer. I have tried with your code but the SetError seem like doesn’t work too.

I want to have message next to the label as following if the field WeightUOM is empty

 

p/s: field WeightUOM is only required when inserting record that why I didn’t include  e.Operation == PXDBOperation.Update 

Userlevel 7
Badge

Hi @mrthanhkhoi were you able to find a solution? Thank you!

Userlevel 3
Badge

Hi @Chris Hackett, Unfortunately I am still unable to set Error message for Selector field. It works for other field types like: text, number, decimal but it doesn’t for Selector field.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved