Custom Validation Message overrides Default Validation Rules

QUESTION 1:
Hi, I created a custom validation rule for catching duplicate emails, but it overrides my default Validation Rules and does not show the validation rules (such as 'Required') for my other columns like 'First Name'. How do I keep my custom validation message without it overriding my default validation rules?

QUESTION 2:
After displaying my custom validation message for duplicate emails, if I change the email value to a non-duplicate, the validation message does not disappear. How do I make the custom validation message go away when it's condition has been satisfied?

I have attached a video in the zip file demonstrating my problem. Thank you

'.razor'


CustomValidation


Attachment: Test_validation_1719d34c.zip

5 Replies 1 reply marked as answer

VN Vignesh Natarajan Syncfusion Team May 6, 2021 11:23 AM UTC

Hi Kenney,  
 
Thanks for contacting Syncfusion support.  
 
Query: “ How do I keep my custom validation message without it overriding my default validation rules? 
 
We have analyzed the reported query and we understand that perform default validation on other column when Custom Validator component is defined to validate the duplicate email. We would like to inform you that when Custom Validator Component is defined inside the Validator of GridEditSettings, default validation will not work. Because in the custom validator component, we have override the HandleValidation method of Validator component and performed validation for Email column. So that method (Handle Validation) only will be triggered when validating the value of other columns as well. 
 
So we request you to handle the validation for other column as well in you validator component to achieve your requirement. Please get back to us if you have further queries.    
 
Query2: “How do I make the custom validation message go away when it's condition has been satisfied? 
 
We are able to reproduce the reported behavior at our end while preparing a sample using your code example. In your Validator component you have only cleared the validation message store. Hence the reported issue has occurred. Once the validation gets success, we request you to call ShowValidationMessage of the context with true value like below  
 
Refer the below code example 
 
protected void HandleValidation(FieldIdentifier identifier) 
        { 
            if (identifier.FieldName.Equals("Email")) 
            { 
                messageStore.Clear(identifier); 
. . . . . . . 
                else 
                { 
                    messageStore.Clear(identifier); 
                    context.ShowValidationMessage("Email"truenull); 
                } 
            } 
        } 
 
 
For your convenience we have prepared a sample using the above code example which can be downloaded from below  
 
 
Refer to our UG documentation for your reference 
 
 
Please get back to us if you have further queries.  
 
Regards, 
Vignesh Natarajan 


Marked as answer

KP Kenney Phan May 7, 2021 05:39 PM UTC

Hi thank you for your response.

How do I implement 'Required' validation rule in my Custom Validator component then?

I want the 'Required' validation message to fire when the user tries to add a new record and presses 'Update' while 'First Name' has value of null.

Thank you


VN Vignesh Natarajan Syncfusion Team May 10, 2021 05:17 AM UTC

Hi  Kenney, 
 
Thanks for the update.  
 
Query: “I want the 'Required' validation message to fire when the user tries to add a new record and presses 'Update' while 'First Name' has value of null 
 
We have analyzed your query and we suggest you to achieve your requirement by modifying the Validator component class like below. We have modified the ValidateRequested method to call HandleValdiation for multiple column and perform validation while inserting a records.  
 
Refer the below code example.  
 
public class MyCustomValidator : ComponentBase{    [Parameter]    public ValidatorTemplateContext context { getset; }     private ValidationMessageStore messageStore;     WeatherForecastService Service = new WeatherForecastService();     [CascadingParameter]    private EditContext CurrentEditContext { getset; }     protected override void OnInitialized()    {        messageStore = new ValidationMessageStore(CurrentEditContext);         CurrentEditContext.OnValidationRequested += ValidateRequested;        CurrentEditContext.OnFieldChanged += ValidateField;    }    protected void HandleValidation(FieldIdentifier identifier)    {        //validate your requirment column        if (identifier.FieldName.Equals("CustomerID"))        {            messageStore.Clear(identifier);             if ((context.EditContext.Model as Order).CustomerID == "" || (context.Data as Order).CustomerID == null)            {                messageStore.Add(identifier, "Customer Name should not be empty");                context.ShowValidationMessage("CustomerID"false"Customer Name should not be empty");            }            else            {                messageStore.Clear(identifier);                context.ShowValidationMessage("CustomerID"truenull);            }        }        if (identifier.FieldName.Equals("OrderID"))        {            messageStore.Clear(identifier);             if ((context.EditContext.Model as Order).OrderID <= 0)            {                messageStore.Add(identifier, "Order ID should not be empty");                context.ShowValidationMessage("OrderID"false"Order ID should not be empty");            }            else            {                messageStore.Clear(identifier);                context.ShowValidationMessage("OrderID"truenull);            }        }        if (identifier.FieldName.Equals("Email"))        {            messageStore.Clear(identifier);            var data = Service.GetOrders();             if ((context.EditContext.Model as Order).Email == "" || (context.EditContext.Model as Order).Email == null)            {                messageStore.Add(identifier, "Email Address should not be empty");                context.ShowValidationMessage("Email"false"Email Address should not be empty");            }            else if (!(context.EditContext.Model as Order).Email.Contains("@"))            {                messageStore.Add(identifier, "Enter the Valid email address");                context.ShowValidationMessage("Email"false"Enter the Valid email address");             }            else            if (data.Where(x => x.Email == (context.EditContext.Model as Order).Email).ToList().Count > 0)            {                messageStore.Add(identifier, "Duplicate Email ID found");                context.ShowValidationMessage("Email"false"Duplicate Email ID found");            }            else            {                messageStore.Clear(identifier);                context.ShowValidationMessage("Email"truenull);            }        }    }    protected void ValidateField(object editContext, FieldChangedEventArgs fieldChangedEventArgs)    {        HandleValidation(fieldChangedEventArgs.FieldIdentifier);    }    private void ValidateRequested(object editContext, ValidationRequestedEventArgs validationEventArgs)    {        //here call the filed you want to validate        string[] fields = new string[] { "CustomerID""OrderID""Email" };        foreach (var field in fields)        {            HandleValidation(CurrentEditContext.Field(field));        }    }}
 
For your convenience we have prepared a sample which can be downloaded from below  
 
Please get back to us if you have further queries.  
  
Regards, 
Vignesh Natarajan 



KP Kenney Phan May 11, 2021 05:44 PM UTC

Thank you!!


VN Vignesh Natarajan Syncfusion Team May 12, 2021 04:27 AM UTC

Hi Kenney,  

Thanks for the update.  

We suspect that you have achieved your requirement using our solution.  

Please get back to us if you have further queries.  

Regards, 
Vignesh Natarajan 


Loader.
Up arrow icon