Skip to main content
Solved

Avoid Save after validation fails.


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

View original
Did this topic help you find an answer to your question?

2 replies

Forum|alt.badge.img+9
  • Semi-Pro III
  • 229 replies
  • 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
  • 1 reply
  • March 21, 2024
sweta68 wrote:

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();

}


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings