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
Hi Robert,
I followed your instructions & created new package 'PackageValidator' with deactivated the "compile into separate assembly" option
Source code:
-------------------
namespace Terrasoft.Configuration
{
using System;
using System.Text.RegularExpressions;
using Terrasoft.Common;
using Terrasoft.Core;
using Terrasoft.Core.DB;
using Terrasoft.Core.Entities;
using Terrasoft.Core.Entities.Events;
[EntityEventListener(SchemaName = "ContactCommunication")]
public class UsrContactCommunicationEventListener : BaseEntityEventListener
{
private Guid GetCommunicationTypeId(UserConnection userConnection, string typeName)
{
var select = new Select(userConnection)
.Top(1)
.Column("Id")
.From("CommunicationType")
.Where("Name").IsEqual(Column.Parameter(typeName)) as Select;
return select.ExecuteScalar<Guid>();
}
public override void OnSaving(object sender, EntityBeforeEventArgs e)
{
base.OnSaving(sender, e);
var entity = (Entity)sender;
var userConnection = entity.UserConnection;
var communicationTypeId = entity.GetTypedColumnValue<Guid>("CommunicationTypeId");
var value = entity.GetTypedColumnValue<string>("Number")?.Trim() ?? string.Empty;
var mobilePhoneTypeId = GetCommunicationTypeId(userConnection, "MobilePhone");
// MobilePhone validation
if (communicationTypeId == mobilePhoneTypeId)
{
string digitsOnly = Regex.Replace(value, @"\D", "");
if (digitsOnly.Length > 10)
{
string message = new LocalizableString(userConnection.ResourceStorage,
"ContactCommunicationEventListener", "LocalizableStrings.MobilePhoneTooLongMessage.Value");
throw new Exception(message);
}
}
}
}
}
It's not working, not validating mobilePhone having lesser or more than 10 digits.
Also, If I change className to 'ContactCommunicationEventListener' then get below error:
(A File already exist with same className in 'CrtBase
' package)
Hello,
we recommend you to create ticket in Creatio Support system addressing your inquiry to the mailbox support@creatio.com, so our team will review the details and process this case accordingly.
Thank you for reaching out!
souresh khandelwal,
did you find out what the issue was?
If so, please let us know!
Thanks and BR,
Robert