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

New Post: Help with chaining custom validator

$
0
0

Yep :(

Just as a test I've created a new blank simple web app (dropbox link for project: https://www.dropbox.com/s/7ol5j5ikg0rx4ie/FluentTest.zip) full code behind looks like:

namespace FluentTest
{
    using System;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Web.UI.HtmlControls;

    using FluentValidation;
    using FluentValidation.Results;
    using FluentValidation.Validators;

    public partial class FluentTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void FindRecord(object sender, EventArgs e)
        {
            try
            {
                HistoricalBilling.Cli = this.findRecordNumber.Text;
                HistoricalBilling.ReportStart = this.findCustomStartDateTime.Text;
                HistoricalBilling.ReportEnd = this.findCustomEndDateTime.Text;

                var historicalBilling = new HistoricalBilling();
                var validator = new HistoricialBillingValidator();

                ValidationResult results = validator.Validate(historicalBilling);

                if (!results.IsValid)
                {
                    var ul = new HtmlGenericControl("ul");

                    foreach (HtmlGenericControl li in
                        results.Errors.Select(
                            failure =>
                            new HtmlGenericControl("li")
                            {
                                InnerText =
                                        "Property " + failure.PropertyName + " failed validation. Error was: "
                                        + failure.ErrorMessage + Environment.NewLine
                            }))
                    {
                        ul.Controls.Add(li);
                    }

                    this.errorPanel.Visible = true;
                    this.successPanel.Visible = false;
                    this.errorPanelText.Text = "Hmmm something doesn't look right...";
                    this.errorPanelList.Controls.Add(ul);
                }
                else
                {
                    this.errorPanel.Visible = false;
                    this.successPanel.Visible = true;
                    this.successPanelText.Text = "Passed validation";

                }
            }
            catch (Exception ex)
            {
            }
        }

    }

    public class HistoricalBilling
    {
        public static string Cli { get; set; }
        public static string ReportEnd { get; set; }
        public static string ReportStart { get; set; }
    }

    public class HistoricialBillingValidator : AbstractValidator<HistoricalBilling>
    {
        public HistoricialBillingValidator()
        {
            this.RuleFor(historicalBilling => HistoricalBilling.Cli)
                .NotNull()
                .NotEmpty()
                .Length(3, 20);
            this.RuleFor(historicalBilling => HistoricalBilling.ReportStart)
                .NotNull()
                .NotEmpty()
                //.EnsureValidDate8601();
                .SetValidator(new ValidDate8601());
            this.RuleFor(historicalBilling => HistoricalBilling.ReportEnd)
                .NotNull()
                .NotEmpty()
                //.EnsureValidDate8601();
                .SetValidator(new ValidDate8601());
        }
    }

    public class ValidDate8601 : PropertyValidator
    {
        public ValidDate8601()
            : base("Property {PropertyName} is not a valid date.")
        {
        }

        protected override bool IsValid(PropertyValidatorContext context)
        {
            var date = context.PropertyValue;

            var regex =
                    new Regex(
                    @"^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$");
            return regex.IsMatch((string)date);
        }
    }

    public class ValidationExtensions
    {
        public IRuleBuilderOptions<T, string> EnsureValidDate8601<T>(IRuleBuilder<T, string> ruleBuilder)
        {
            return ruleBuilder.SetValidator(new ValidDate8601());
        }
    }
}

The call to .SetValidator(new ValidDate8601()); works fine and if I replace with .EnsureValidDate8601(); I get the cannot resolve symbol with error: 'FluentValidation.IRuleBuilderOptions<FluentTest.HistoricalBilling,string>' does not contain a definition for 'EnsureValidDate8601' and no extension method 'EnsureValidDate8601' accepting a first argument of type 'FluentValidation.IRuleBuilderOptions<FluentTest.HistoricalBilling,string>' could be found

 

Thanks for the assistance so far!


Viewing all articles
Browse latest Browse all 1917

Trending Articles



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