Dynamic Form Fields

Mar 21, 2013 at 8:34 PM
I have a client requirement to build a dynamic form. I am building the form using a list of the following:
    public class FieldViewModel
    {
        public string FieldName { get; set; }
        public string FieldValue { get; set; }
        public bool IsDate { get; set; }
        public int? MaxLength { get; set; }
    }
What I want to do is define a validator such that FieldViewModel.FieldValue is validated against FieldViewModel.MaxLength and FieldViewModel.IsDate

Is this possible with FluentValidation?
Coordinator
Mar 22, 2013 at 12:27 PM
Yes, that should be possible, although you'll need to use Must rules rather than the standard length rules etc. Something like this should probably work:
public class FieldModelValidator : AbstractValidator<FieldViewModel> {
    public FieldModelValidator() {
        RuleFor(x => x.FieldValue).Must(BeADate).When(x => x.IsDate);
        RuleFor(x => x.FieldValue).Must(NotExceedLength).When(x => x.Maxlength != null);
    }

    bool NotExceedLength(FieldViewModel model, string value) {
        return value != null && value.Length <= model.MaxLength.Value;
    }

    private bool BeADate(string value) {
        DateTime d;
        return DateTime.TryParse(value, out d);
    }
}
Jeremy
Mar 22, 2013 at 6:28 PM
Of course! I was certainly overcomplicating the problem in my mind...

Thanks for the awesome support!
Jan 31 at 3:52 PM
@Jeremy
I test your code. it works good . but the problem is we lost client validetion.

in fact i have
 public class FieldViewModel
    {
        public string FieldName { get; set; }
        public string FieldValue { get; set; }
        public List<int> Validations { get; set; } // list of validation must applied to FieldValue for example : 1 required , 2 Email , ... 
     
    }
I feel FieldName and Validations of FieldViewModel from database
I want to have both Client and Server side validation for FieldValue
how can do this?
Coordinator
Jan 31 at 9:25 PM
Hi

This isn't possible I'm afraid - this code is server-side only.
Feb 2 at 6:29 PM
I've solved my issue by inject all attributes that jQuery.Unobtrusive.Validation needs manually
I think this can be integrated by fluent validation in next version!
All reseon i've used fluent validation because of dynamic validation for making form builder
Feb 16 at 9:23 AM
Edited Feb 16 at 9:25 AM
@Jeremy

I want to have range validation in my class that value of my range defined in my class
public class FieldViewModel
    {
        public string FieldName { get; set; }
        public string FieldValue { get; set; }
        public string MinRange { get; set; }
        public string MaxRange { get; set; }
        public List<int> Validations { get; set; } 
     
    }
how to get value of MinRange and MaxRange of my class to pass to
InclusiveBetween(MinRange ,MaxRange )
public class FieldModelValidator : AbstractValidator<FieldViewModel> {
//!!!--MinRange and MaxRange  not recognized in InclusiveBetween
 RuleFor(x => x.FieldValue ) .InclusiveBetween(MinRange ,MaxRange ) .When(w => w.Validations.Any(x => x.Validations.Contain(2) ))

}
Coordinator
Feb 17 at 9:17 AM
Hi

InclusiveBetween only takes hard-coded numbers. You'd need to use a Must rule if you want to access other properties within the object.

Jeremy