blazor-validation
blazor-validation copied to clipboard
Nested RuleForEach: GetParentObjectAndPropertyName fails to find the PropertyInfo
public class DemographicValidator : AbstractValidator<AppContext>
{
public DemographicValidator()
{
RuleForEach(x => x.Assignments).ChildRules(x =>
{
x.RuleForEach(x => x.StaffHistory).ChildRules(x =>
{
x.RuleFor(x => x.StartDate).NotEmpty().WithMessage("Start Date is required.");
});
}
}
}
I've omitted most of the rest of the validation for brevity, as that all worked without issue until this one. Once it gets here it throws a null reference error when trying to get property.GetValue(model, null) because property is null.
private void GetParentObjectAndPropertyName(object model, string propertyPath, out object parentObject, out string propertyName)
{
if (model == null)
{
throw new ArgumentNullException("model");
}
Queue<string> queue = new Queue<string>(propertyPath.Split('.'));
Type type = model.GetType();
while (queue.Count > 1)
{
string text = queue.Dequeue();
string text2 = null;
int num = text.IndexOf('[');
if (num > 0)
{
text2 = text.Substring(num + 1, text.Length - num - 2);
text = text.Remove(num);
}
PropertyInfo property = type.GetProperty(text);
model = ((model == null) ? null : property.GetValue(model, null));
if (text2 == null)
{
type = property.PropertyType;
continue;
}
List<object> list = ((IEnumerable<object>)model).ToList();
int index = int.Parse(text2);
model = list[index];
}
parentObject = model;
propertyName = queue.Dequeue();
}
It looks like it's trying to get AppContext.StaffHistory instead of Assignment.StaffHistory.