Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Required If Validations #7408


User avatar
0
instaemi created

We are using the Asp.Net Core Mvc 5+jquery application we are trying to implement Required If Valiadtion but it is not working our application and ddl also not supporting how to acheive the Required If Suppose one filed is data some set fileds are required other wise it not required we are following this link https://blogs.msdn.microsoft.com/stuartleeks/2011/10/06/flexible-conditional-validation-with-asp-net-mvc-3/


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Can you share your RequiredIfAttribute code?

  • User Avatar
    0
    instaemi created
    public class RequiredIfAttribute : ValidationAttribute
     {
             private readonly string _condition;
             public RequiredIfAttribute(string condition)
             {
                 _condition = condition;
             }
             protected override ValidationResult IsValid(object value, ValidationContext validationContext)
             {
                 Delegate conditionFunction = CreateExpression(
     validationContext.ObjectType, _condition);
                 bool conditionMet = (bool) conditionFunction.DynamicInvoke(
     validationContext.ObjectInstance);
                 if (conditionMet)
                 {
                     if (value == null)
                     {
                         return new ValidationResult(FormatErrorMessage(null));
                     }
                 }
                 return null;
             }
             private Delegate CreateExpression(Type objectType, string expression)
             {
                 // TODO - add caching
                 LambdaExpression lambdaExpression =
                          System.Linq.Dynamic.DynamicExpression.ParseLambda( 
                                    objectType, typeof (bool), expression);
                 Delegate func = lambdaExpression.Compile();
                 return func;
             }
         }
    

    these code we need "DynamicQuery" dll when we install core-shared libary we get the build errors in mobile projects

  • User Avatar
    0
    maliming created
    Support Team

    Does the Abp parameter validation component call the IsValid method of the RequiredIfAttribute class?

    Does DynamicQuery have no nuget packages? It is not recommended to use DLLs to manage project dependencies.