We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Calculating validation rule dynamically

Hello,
I'm trying to dynamically configure grid's date column range(or min/max) validation rule based on another date column, for e.g.:
Date A = 20.03.2021
Date B range must be between Date A and current date.
How could I achieve that?

Best Regards
Jacob

5 Replies 1 reply marked as answer

RS Renjith Singh Rajendran Syncfusion Team March 23, 2021 07:19 AM UTC

Hi Boucher, 

Greetings from Syncfusion support. 
 
We can achieve this requirement by using DataAnnotation and apply custom validation for the column. With this you can perform custom validation based on the value you get in the IsValid method based on your requirement. Please refer and use as like the below codes to achieve custom validation for the OrderDate column. 

 
<GridColumn Field=@nameof(Order.OrderDate)  TextAlign="TextAlign.Right" Width="120"></GridColumn> 
 
public static bool ValidFlag; 
[AttributeUsage(AttributeTargets.Property)] 
public class OrderDateValidationAttribute : ValidationAttribute 
{ 
    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
        // Here you can perform your custom validation and return true/false value 
        // you will get the entered OrderDate value in “value” and validationContext will have other row cell data 
        ... 
        return ValidFlag ? new ValidationResult("Custom Message"new[] { validationContext.MemberName }) : ValidationResult.Success; 
    } 
} 
 
public class Order 
{ 
    public int? OrderID { getset; } 
    public string CustomerID { getset; } 
    [OrderDateValidation] 
    public DateTime? OrderDate { getset; } 
    public DateTime? ShippedDate { getset; } 
    public string ShipCountry { getset; } 
} 


Please get back to us if you need further assistance. 

Regards, 
Renjith Singh Rajendran 




JA Jakub March 24, 2021 01:01 AM UTC

Hey Renjith,
Thanks for your reply.
Unfortunately I'm still unable to achieve my goal after implementing your suggestions.

I made following changes to my project:
  • Implemented custom Data Annotation Validator
  • Marked my model property with it

Here is my Grid component definition in razor component:

While debugging I can see that custom validator is hit and logic is executed properly, but on the front-end side of application date which failed the validation is placed into cell and marked with green border line:

Did I miss something?



RS Renjith Singh Rajendran Syncfusion Team March 24, 2021 07:24 AM UTC

Hi Jakub, 

We have analyzed the shared codes and we could see that, you have set ValidFlag as false in both if else cases. So as the ValidFlag is false, the Validation success will be applied for the cell. And as the validation became success, the message is not shown. 

We suggest you to ensure this scenario from your side. Please refer the codes below, 

 
[AttributeUsage(AttributeTargets.Property)] 
public class OrderDateValidationAttribute : ValidationAttribute 
{ 
    public static bool ValidFlag = false; 
    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
        string ErrorMessage = ""; 
       ... 
        if(returnDate < borrowDate) 
        { 
            ValidFlag = true; 
            ErrorMessage = "..."; 
        } 
        else if(returnDate > DateTime.Now) 
        { 
            ValidFlag = true; 
            ErrorMessage = "..."; 
        } 
        return ValidFlag ? new ValidationResult(ErrorMessage, new[] { validationContext.MemberName }) : ValidationResult.Success; 
    } 
} 


Please get back to us if you need further assistance. 

Regards, 
Renjith R 


Marked as answer

JA Jakub March 24, 2021 05:22 PM UTC

Now it's working as expected, thank you Renjith!
Previously I got a little misled by your's variable name and return combination, in fact when ValidFlag was false (which I assumed as invalid entry) it returned ValidationResult.Success.




RS Renjith Singh Rajendran Syncfusion Team March 25, 2021 05:25 AM UTC

Hi Jakub, 

We are glad to hear that your requirement is achieved. Please get back to us if you need further assistance. 

Regards, 
Renjith R 


Loader.
Live Chat Icon For mobile
Up arrow icon