Important!!! How to Validate Communication Options (MobilePhone, Email)
Hi Team,
I'm currently working on implementing validation for fields like MobilePhone, Email, and HomePhone under the Communication Options section on the Contact_FormPage
.
However, I couldn’t find any relevant component or configuration in the Contact_FormPage
source code that would allow me to add validation using handler or validator methods directly.
I considered using an Entity Event Listener to handle the validation logic, but I noticed that an event handler file already exists in the CrtBase
package. Since this file is part of a system package, I’m unable to modify it.
(source-code)
namespace Terrasoft.Configuration
{
using System;
using Terrasoft.Common;
using Terrasoft.Core;
using Terrasoft.Core.DB;
using Terrasoft.Core.Entities;
using Terrasoft.Core.Entities.Events;
using Terrasoft.Core.ExternalUsers;
using Terrasoft.Core.Factories;
#region Class: ContactCommunicationEventListener
[EntityEventListener(SchemaName = "ContactCommunication")]
public class ContactCommunicationEventListener: BaseEntityEventListener
{
#region Methods: Private
private bool GetIsExternalUserCommunication(Guid contactId, UserConnection userConnection) {
var select = new Select(userConnection)
.Count("Id")
.From("SysAdminUnit")
.Where("ContactId").IsEqual(Column.Parameter(contactId))
.And("ConnectionType").IsEqual(Column.Parameter(UserType.SSP)) as Select;
var externalUsersCount = select.ExecuteScalar();
return externalUsersCount != 0;
}
#endregion
#region Methods: Public
public override void OnSaving(object sender, EntityBeforeEventArgs e) {
base.OnSaving(sender, e);
var entity = (Entity)sender;
var communicationTypeId = entity.GetTypedColumnValue("CommunicationTypeId");
if (communicationTypeId != Guid.Parse(CommunicationTypeConsts.EmailId)) {
return;
}
var contactId = entity.GetTypedColumnValue("ContactId");
var userConnection = entity.UserConnection;
if (!GetIsExternalUserCommunication(contactId, userConnection)) {
return;
}
var email = entity.GetTypedColumnValue("Number");
var emailValidator = ClassFactory.Get();
var isEmailValid = emailValidator.IsValidEmailDomainForExternalUser(email);
if (!isEmailValid) {
string message = new LocalizableString(userConnection.ResourceStorage,
"ContactCommunicationEventListener", "LocalizableStrings.ErrorMessage.Value");
throw new SSPRestrictedEmailDomainException(message);
}
}
#endregion
}
#endregion
}
Could you please advise on:
- The recommended approach to implement validation for these communication fields?
- Whether there’s a supported way to override or extend existing entity event logic from a system package?
Like
Hello,
Yesterday, I created a post regarding this issue here: How to add validator to phone number fields in communication options? | Community Creatio
Oleg already answered, but I couldn't figure it out...maybe you have more luck!
If you want to override the entity event listener, you have to deactivate the "compile into separate assembly" option in your package. This will ensure that your changes are included in the same assembly as the rest of the out-of-the-box configuration.
BR,
Robert