0

Current project:

  • ASP.NET 4.5.2
  • MVC 5
  • Fluent Validation

I may have a very strange issue here. I may be experiencing “Validation Leakage”, whereby validation on one sub-model (which is supposed to be completely filled out) is “leaking over” into the same model imported a second and third time, but where it does not have to be filled out.

So I have this sign-up form, which occurs after registration. Without filling in the form, a user cannot get into the site. Essentially, a profile page that is vital for the proper operation of the site internals.

One class of user, called a Recruiter, needs three “Addresses”: for the company, for them as a Contact (in case their office is different), and for Billing (in case the Billing location is different). Only the Company address is required, in that it needs an address manually entered. If the second and third are the same as the first, the user can flag them specifically via booleans controlled by Bootstrap Switch, and the second and third sub-models can be submitted as empty. Validation only needs to occur on the first sub-model if either of the two booleans (“this address same as main address”) are set to True (this is important!)

My original solution (which I have since abandoned) is to have the Address sub-model accurately reflect the DB model, in that it has not only the physical address, but also phone number, eMail, etc., etc.. I abandoned it because I was experiencing validation issues with the entire Address model, in that the two secondary sub-models kept getting validated even though my validators were only supposed to get triggered when the addresses were different (I used a Bootstrap Switch to allow these address fields to be hidden as groups, so the user could un-hide the address fields if the second and third addresses really were different).

I have since redesigned the Address sub-model to include only the physical address: Street, city, state, etc.. Unfortunately, I am still experiencing the problem, in that having the second and third sub-form as empty has validation thrown on them, even when the user leaves them in the default config (this address is same as main address -- do not validate).

My Address model:

public class CreateRecruiterAddressModel {
  [DisplayName("Address")]
  public string Address1 { get; set; }
  [DisplayName("P.O. Box, Suite, etc.")]
  public string Address2 { get; set; }
  [DisplayName("City")]
  public string City { get; set; }
  #region For US and Canada
  [DisplayName("Country")]
  public Guid CountryId { get; set; }
  #endregion
  #region For US
  [DisplayName("State")]
  public Guid? StateId { get; set; }
  [DisplayName("Zip Code")]
  [DataType(DataType.PostalCode)]
  public string ZipCode { get; set; }
  #endregion
  #region for Canada
  [DisplayName("Province")]
  public Guid? ProvinceId { get; set; }
  [DisplayName("Postal Code")]
  [DataType(DataType.PostalCode)]
  public string PostalCode { get; set; }
  #endregion
  #region For everyone else
  [DisplayName("Province")]
  public string ProvinceName { get; set; }
  [DisplayName("Country")]
  public string CountryName { get; set; }
  [DisplayName("Postal Code")]
  [DataType(DataType.PostalCode)]
  public string Postal { get; set; }
  #endregion
}

As you can see, I am providing sections for decision-making: if a user is from Canada or the US, they get a custom drop-down for Province/State, and anyone else gets plain Country and Province text fields.

My Recruiter model:

public class CreateRecruiterProfileViewModel {
  [DisplayName("Company Name")]
  public string CompanyName { get; set; }

  public CreateRecruiterAddressModel mailingAddress { get; set; }

  [DisplayName("Phone Number")]
  [DataType(DataType.PhoneNumber)]
  public string MailingPhone { get; set; }
  [DisplayName("Fax Number")]
  [DataType(DataType.PhoneNumber)]
  public string MailingFax { get; set; }

  [DisplayName("Contact Name")]
  public string ContactName { get; set; }
  [DisplayName("Is your contact address at this company the same as the company’s mailing address, above?")]
  public bool ContactSameAsAddress { get; set; }

  public CreateRecruiterAddressModel contactAddress { get; set; }

  [DisplayName("Phone Number")]
  [DataType(DataType.PhoneNumber)]
  public string ContactPhone { get; set; }
  [DisplayName("Extension")]
  public short? ContactExtension { get; set; }
  [DisplayName("eMail:")]
  public string ContacteMail { get; set; }

  [DisplayName("Name on the credit card")]
  public string BillingName { get; set; }
  [DisplayName("Is the billing address for this account the same as the company’s mailing address, above?")]
  public bool BillingSameAsAddress { get; set; }

  public CreateRecruiterAddressModel billingAddress { get; set; }

  [DisplayName("Phone Number")]
  [DataType(DataType.PhoneNumber)]
  public string BillingPhone { get; set; }
  [DisplayName("Extension")]
  public short? BillingExtension { get; set; }
  [DisplayName("eMail:")]
  public string BillingeMail { get; set; }

  [DisplayName("Website")]
  public string Website { get; set; }
  [DisplayName("Industry")]
  public string IndustryId { get; set; }
  [DisplayName("Number of Employees:")]
  public string NumberEmployees { get; set; }
  [DisplayName("Operating Since:")]
  [DataType(DataType.Date)]
  public DateTime? OperatingSince { get; set; }
  [DisplayName("Operating Revenue:")]
  public string OperatingRevenue { get; set; }


  public Guid CountryNU {
    get { return Settings.Default.CountryNU; }
  }
  public Guid CountryCA {
    get { return Settings.Default.CountryCA; }
  }
  public Guid CountryUS {
    get { return Settings.Default.CountryUS; }
  }
  private IEnumerable<SelectListItem> _CountryList;
  public IEnumerable<SelectListItem> CountryList {
    get { return SelectLists.CountryList(); }
    set { _CountryList = value; }
  }
  private IEnumerable<SelectListItem> _StateList;
  public IEnumerable<SelectListItem> StateList {
    get { return SelectLists.ProvinceList(Settings.Default.CountryUS); } // Add US Guid 
    set { _StateList = value; }
  }
  private IEnumerable<SelectListItem> _ProvinceList;
  public IEnumerable<SelectListItem> ProvinceList {
    get { return SelectLists.ProvinceList(Settings.Default.CountryCA); } // Add CA Guid 
    set { _ProvinceList = value; }
  }
  private IEnumerable<SelectListItem> _IndustryList;
  public IEnumerable<SelectListItem> IndustryList {
    get { return SelectLists.IndustryList(); }
    set { _IndustryList = value; }
  }

  public CreateRecruiterProfileViewModel() {
    ContactSameAsAddress = true;
    BillingSameAsAddress = true;
  }
}

Lotsa stuff there, sorry for the data dump. Probably only the first half is important.

My Address validation:

public class CreateRecruiterAddressValidator : AbstractValidator<CreateRecruiterAddressModel> {
  public CreateRecruiterAddressValidator() {
    RuleFor(x => x.Address1)
      .NotEmpty().WithMessage("Please provide the current address.")
      .Length(6, 128).WithMessage("Addresses should be between 6 and 128 characters long.");
    RuleFor(x => x.City)
      .NotEmpty().WithMessage("Please provide the current city.")
      .Length(2, 64).WithMessage("City names should be between 2 and 64 characters long.");
    RuleFor(x => x.CountryId)
      .NotEmpty().WithMessage("Please choose the current country.");
    When(x => x.CountryId == Settings.Default.CountryCA,
      () => {
        RuleFor(x => x.ProvinceId)
          .NotEmpty().WithMessage("Please choose the current province.");
        RuleFor(x => x.PostalCode)
          .NotEmpty().WithMessage("Please enter a valid postal code.")
          .Length(7, 7).WithMessage("Postal code must be in the form of &#8220;X1X-1X1&#8221;.")
          .Matches(@"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])-(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$").WithMessage("Postal code must be Canada-valid, in the form of &#8220;X1X-1X1&#8221;");
      });
    When(x => x.CountryId == Settings.Default.CountryUS,
      () => {
        RuleFor(x => x.StateId)
          .NotEmpty().WithMessage("Please choose the current state.");
        RuleFor(x => x.ZipCode)
          .NotEmpty().WithMessage("Please enter a valid zip code.")
          .Length(5, 10).WithMessage("Zip code must be in the form of &#8220;12345&#8221 or &#8220;12345-6789&#8221;.")
          .Matches(@"^\d{5}(?:[-\s]\d{4})?$").WithMessage("Zip code must be US-valid, in the form of &#8220;12345&#8221 or &#8220;12345-6789&#8221;.");
      });
    When(x => x.CountryId == Settings.Default.CountryNU,
      () => {
        RuleFor(x => x.CountryName)
          .NotEmpty().WithMessage("Please provide a valid country name.")
          .Matches(@"[a-zA-Z\u00C0-\u01FF- ]+").WithMessage("Please enter only letters and spaces, no numbers or special characters.")
          .Length(2, 64).WithMessage("Country names should be between 2 and 64 characters long.");
        RuleFor(x => x.ProvinceName)
          .NotEmpty().WithMessage("Please provide a valid province name.")
          .Matches(@"[a-zA-Z\u00C0-\u01FF- ]+").WithMessage("Please enter only letters and spaces, no numbers or special characters.")
          .Length(2, 64).WithMessage("Province names should be between 2 and 64 characters long.");
        RuleFor(x => x.Postal)
          .NotEmpty().WithMessage("Please provide a valid postal code.")
          .Length(3, 10).WithMessage("Postal code must be between 3 and 10 digits long, and valid for the country of residence.");
      });
  }
}

And now for my Recruiter validation:

public class CreateRecruiterProfileValidator : AbstractValidator<CreateRecruiterProfileViewModel> {
  public CreateRecruiterProfileValidator() {
    RuleFor(x => x.CompanyName)
      .NotEmpty().WithMessage("Please provide a valid company name.")
      .Length(2, 64).WithMessage("Company names should be between 2 and 64 characters long.");

    RuleFor(x => x.mailingAddress)
  .SetValidator(new CreateRecruiterAddressValidator()); // This is the validator I think screws up the bottom two

    RuleFor(x => x.ContactName)
      .NotEmpty().WithMessage("Please provide a valid contact name.")
      .Length(2, 64).WithMessage("Contact names should be between 2 and 64 characters long.");

    RuleFor(x => x.contactAddress)
      .SetValidator(new CreateRecruiterAddressValidator())
      .When(x => x.ContactSameAsAddress == false); // Problem one

    RuleFor(x => x.ContactPhone)
      .NotEmpty().WithMessage("Please enter a valid 10-digit phone number.")
      .Length(12, 12).WithMessage("Phone number must be in the form of &#8220;123-456-7890&#8221;")
      .Matches(@"^\d{3}-\d{3}-\d{4}$").WithMessage("Phone number must be a valid 10-digit phone number with dashes, in the form of &#8220;123-456-7890&#8221;");
    RuleFor(x => x.ContacteMail)
      .NotEmpty().WithMessage("Please enter a valid eMail Address..")
      .EmailAddress().WithMessage("Please provide a valid eMail address.");
    RuleFor(x => x.BillingName)
      .NotEmpty().WithMessage("Please provide a valid billing name.")
      .Length(2, 64).WithMessage("Billing names should be between 2 and 64 characters long.");

    RuleFor(x => x.billingAddress)
      .SetValidator(new CreateRecruiterAddressValidator())
      .When(x => x.BillingSameAsAddress == false); // Problem two

    RuleFor(x => x.BillingPhone)
      .NotEmpty().WithMessage("Please enter a valid 10-digit phone number.")
      .Length(12, 12).WithMessage("Phone number must be in the form of &#8220;123-456-7890&#8221;")
      .Matches(@"^\d{3}-\d{3}-\d{4}$").WithMessage("Phone number must be a valid 10-digit phone number with dashes, in the form of &#8220;123-456-7890&#8221;");
    RuleFor(x => x.BillingeMail)
      .NotEmpty().WithMessage("Please enter a valid eMail Address..")
      .EmailAddress().WithMessage("Please provide a valid eMail address.");
    When(x => !string.IsNullOrEmpty(x.Website),
      () => {
        RuleFor(x => x.Website)
          .Length(12, 64).WithMessage("A URL should be in the form of “http://www.domain.com/”")
          .Matches(@"^http[s]?:\/\/").WithMessage("A URL should begin with “http://” or “https://”");
      });
    RuleFor(x => x.IndustryId)
      .NotEmpty().WithMessage("Please choose the closest appropriate industry.");
  }
}

The way I see the validation, the .SetValidator() for the Contact and Billing address models should only fire when the boolean flags are set to false, but they appear to fire every single time, regardless of the boolean flag. As in, the form is being returned by the server (server-side triggering) with the Contact and Billing addresses being flagged as required to be filled in. And that is not what I want!

The Address model is not being decorated by any Validation attribute, so the only thing I can think of is that the .SetValidator() validation for the MailingAddress is “leaking over” into the Contact and Billing models.

I have confirmed this by explicitly disabling the Validation calls for both Contact and Billing in the Recruiter's validation (the .SetValidator()) - and the form can be successfully submitted with empty sub-models when I do that. Problem is, if the boolean gets set to False for either of these, I need to be able to validate them to ensure the address is correct and complete.

How can I overcome this?


Crazy Thought:

I just realized that this problem might be hitting me from either end:

  • Because the imported models are using the same field names, the validator for the mailingAddress AddressModel is catching the other field names in the other models.
  • Because the different forms are using the same validator, it is already loaded via mailingAddress, and as such it is already loaded and ready to validate once contactAddress and billingAddress get processed. So they get caught up in the validation even though their own validators don’t get explicitly loaded.

Or maybe I'm getting hit from both ends at once. I would love an expert opinion here.


Update:

On second thought, I don’t think it is either ‘crazy thought’ that is the issue here, because when I explicitly remove the sub-model .SetValidator() validator assigning entirely from the primary Recruiter validation, the validation issue vanishes. Empty Contact and Billing address sub-models can be successfully submitted and processed. If either of my two thoughts above were at play, this wouldn’t happen.

Problem is - if these are not empty, I do need them to be validated! So why is the boolean not being properly used to determine whether or not to assign validation to these sub-forms? Why does any inclusion of a sub-model validator, even when it is supposed to be triggered only by a false boolean value, causing the sub-models to get validated?

1
  • What about instead of when clause, use a normal if and only setup validation if it pass?
    – Cat_Clan
    Aug 12, 2016 at 23:21

1 Answer 1

0

You could try with:

When(x => x.ContactSameAsAddress == false, () => { 
  RuleFor(x => x.contactAddress)
  .SetValidator(new CreateRecruiterAddressValidator())
});
3
  • Sorry I didn’t include that example in my original post, but that was my first version. And no, validation still fired on contactAddress even when ContactSameAsAddress was true. Aug 14, 2016 at 2:18
  • Then what about old plain C# with an IF clause?
    – Cat_Clan
    Aug 16, 2016 at 17:32
  • That doesn’t work within a validator; only explicit FluentValidation terms work there. I tried that, too. Aug 16, 2016 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.