Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Dynamic Form Fields

$
0
0
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

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>