Add validation message in Entity Events layer

Hello,

I am trying to validate a record before it gets inserted into the DB using entity events layer :  public override void OnInserting(object sender, EntityBeforeEventArgs e);

 

If the validation fails, I would like to display a message to the user. Is there a built in method like set validation message or something? I understand this can be done through Web socket but I would prefer if I am able to use a built in validator.

 

Thanks

Like 0

Like

2 comments
Best reply

Hello Shivani,

You can simply throw an exception from the event and it will halt the insert process and display the exception message to the user.

An an example, this OnInserting event checks to ensure the Also known as field is not empty:

using System;
using Terrasoft.Core.Entities;
using Terrasoft.Core.Entities.Events;
 
namespace FX.EntityEventListeners
{
    [EntityEventListener(SchemaName = "Account")]
    public class UsrAccountEntityEvents : BaseEntityEventListener
    {
        public override void OnInserting(object sender, EntityBeforeEventArgs e)
        {
            base.OnInserting(sender, e);
            var account = (Entity)sender;
 
            if (string.IsNullOrEmpty(account.GetTypedColumnValue<string>("AlternativeName"))) 
            {
                throw new Exception("Also known as cannot be blank");
            }
        }
    }
}

This displays this message when the user inserts an account without this field populated:

Ryan

Hello Shivani,

You can simply throw an exception from the event and it will halt the insert process and display the exception message to the user.

An an example, this OnInserting event checks to ensure the Also known as field is not empty:

using System;
using Terrasoft.Core.Entities;
using Terrasoft.Core.Entities.Events;
 
namespace FX.EntityEventListeners
{
    [EntityEventListener(SchemaName = "Account")]
    public class UsrAccountEntityEvents : BaseEntityEventListener
    {
        public override void OnInserting(object sender, EntityBeforeEventArgs e)
        {
            base.OnInserting(sender, e);
            var account = (Entity)sender;
 
            if (string.IsNullOrEmpty(account.GetTypedColumnValue<string>("AlternativeName"))) 
            {
                throw new Exception("Also known as cannot be blank");
            }
        }
    }
}

This displays this message when the user inserts an account without this field populated:

Ryan

 

Thanks Ryan! This works!

Show all comments