Shinigami
Shinigami
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
I'm glad i had this convo
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
but thanks man! I feel like i really understood this stuff well enough to explain it back to someone
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
ideally they should not be nullable, like i was saying i'm just learning as of now following tutorials and stuff
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
yes, sir. I'm currently learning this but i believe giving 2 different error from 2 properties sounds independent and i think that's how it should be
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
this
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
trying to fetch from date and the validationattribute is on to date property
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
this is the whole code
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
public class DateRangeValidatorAttribute : ValidationAttribute
{
public string? PropertyName { get; set; }

public DateRangeValidatorAttribute(string OtherPropertyName)
{
PropertyName = OtherPropertyName;
}
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (value != null)
{
DateTime to_date = Convert.ToDateTime(value);

if (PropertyName != null)
{
var model = (User)validationContext.ObjectInstance;
PropertyInfo? otherproperty = validationContext.ObjectType.GetProperty(PropertyName);

DateTime from_date = Convert.ToDateTime(otherproperty.GetValue(validationContext.ObjectInstance));

if(from_date < to_date)
{
return ValidationResult.Success;
}
}
{
return new ValidationResult("Please provide From Date");
}
}
else
{
return new ValidationResult("Please provide To Date");
}
}

}
}
public class DateRangeValidatorAttribute : ValidationAttribute
{
public string? PropertyName { get; set; }

public DateRangeValidatorAttribute(string OtherPropertyName)
{
PropertyName = OtherPropertyName;
}
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (value != null)
{
DateTime to_date = Convert.ToDateTime(value);

if (PropertyName != null)
{
var model = (User)validationContext.ObjectInstance;
PropertyInfo? otherproperty = validationContext.ObjectType.GetProperty(PropertyName);

DateTime from_date = Convert.ToDateTime(otherproperty.GetValue(validationContext.ObjectInstance));

if(from_date < to_date)
{
return ValidationResult.Success;
}
}
{
return new ValidationResult("Please provide From Date");
}
}
else
{
return new ValidationResult("Please provide To Date");
}
}

}
}
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
so does that piece of code mean, get me the value of "otherproperty" from validationcontext.objectinstance?
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
otherproperty basically has info about the property we want from the objectinstance
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
yes yes, i just have 1 more doubt regarding this piece of code otherproperty.GetValue(validationContext.ObjectInstance)
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
casting your objectinstace to your model is easy and convenient like you did
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
exactly, i have the whole method, let me put it in here
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
alsoo, how are you directly accessing the value from ObjectInstance? is that because you casted it? var model = (PriorityServiceRegister)validationContext.ObjectInstance
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
otherproperty.GetValue(validationContext.ObjectInstance) what does this code mean? why can't we just otherproperty.GetValue("propertyname") do this?
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
now to see the actual value of the a property which we fetched using objectype, we need to use object instance?
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
now the validationcontext also has 2 more properties called ObjectType and ObjectInstance and we use objecttype to see all the properties/methods in the class and if needed we can get the reference of a single property as well
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
so the validationcontext basically has info regarding property called name right? like it's display name and membername etc..
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
let's say i have a property called name and i need to validate it
31 replies
CC#
Created by Shinigami on 11/17/2023 in #help
✅ ValidationContext in ASP.NET
ohkayy, let me tell you what i understood
31 replies