Skip to main content
Answer

Avoid Save after validation fails.

  • October 19, 2023
  • 2 replies
  • 288 views

Forum|alt.badge.img

Hi All,

I need to avoid the data saving once validation fails. I wrote a method in RowPersisting event. Validation message comes but data is going to save .So I need to return from the saving until validation sucess.

Here is my code.

protected void _(Events.RowPersisting<HMRCAPIConfigDetail> e)
        {
            HMRCAPIConfigDetail row = e.Row;
            if(row != null)
            {
                if(String.IsNullOrEmpty(row.Description))
                {
                    string msg = "Description Cannot be Empty";                    
                    WebDialogResult result = HMRCAPIConfigDetails.Ask(ActionsMessages.Warning, msg, MessageButtons.OK, MessageIcon.Warning, true);
                    return;
                }                
            }            
        }

Thanks 

Best answer by sweta68

Hi @bhagyat25 ,

Just add e.Cancel = true; to your code after warning message which will prevents the data saving.

Or else you can use below code snippet to display error message.

 throw new PXSetPropertyException(msg, PXErrorLevel.RowError);

Regards,

Sweta

2 replies

Forum|alt.badge.img+9
  • Semi-Pro III
  • Answer
  • October 19, 2023

Hi @bhagyat25 ,

Just add e.Cancel = true; to your code after warning message which will prevents the data saving.

Or else you can use below code snippet to display error message.

 throw new PXSetPropertyException(msg, PXErrorLevel.RowError);

Regards,

Sweta


  • Freshman I
  • March 21, 2024

Hi @bhagyat25 ,

Just add e.Cancel = true; to your code after warning message which will prevents the data saving.

Sweta

No it won’t prevent it. What it will do is rollback any changes made. If that is not something desired (it’ll nuke any modified/added data) the following may work better instead of RowPersisting event:

 

[PXOverride]
public void Persist(Action baseMethod) {

    var cancel = false;    

    // Do your validation setting the ‘cancel’ to true if it fails

    if ( !cancel )

        baseMethod();

}