FluentValidation: Check if one of two fields are empty

Cybercop picture Cybercop · Jan 14, 2014 · Viewed 41.6k times · Source

I have this model

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }  
}

I want to create a validation where either FirstName or LastName must be filled in by user. I installed FluentValidation and created a customvalidator class

public class PersonValidator:AbstractValidator<Person>
{
    public PersonValidator()
    {
        RuleFor((person=>person.FirstName)//don't know how to check if one is empty
    }
}

To check just one field I could just do RuleFor(person => person.FirstName).NotNull();

But how do I check if one of them is null.

Also, is it possible, once validation is created via fluentValidation, use it on the client side to show error?

Edit1

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        FluentValidationModelValidatorProvider.Configure();
    }
//creating validation
namespace WebApplication1.Models.CustomValidator
{
    public class PersonValidator:AbstractValidator<Person>
    {
        public PersonValidator()
        {
            RuleFor(m => m.FirstName).NotEmpty().When(m => string.IsNullOrEmpty(m.LastName)).WithMessage("*Either First Name or Last Name is required");
            RuleFor(m => m.LastName).NotEmpty().When(m => string.IsNullOrEmpty(m.FirstName)).WithMessage("*Either First Name or Last Name is required");
        }
    }

}
//model class
[Validator(typeof(PersonValidator))]
public class Person
{
    public Person()
    {
        InterestList = new List<string>();
    }
    public int Id { get; set; }
    public int ContactId { get; set; }
    [RequiredIfEmpty("LastName")]
    public string FirstName { get; set; }
    [RequiredIfEmpty("FirstName")]
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public string Phone { get; set; }
    public string Country { get; set; }
    public List<string> InterestList { get; set; } 
}
//view
@model WebApplication1.Models.Person

<script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

@Html.ValidationSummary(true)
@using(Html.BeginForm("AddPerson","Person",FormMethod.Post))
{
    <div class="label">First Name</div>
    <div class="input-block-level">@Html.TextBoxFor(m=>m.FirstName)@Html.ValidationMessageFor(m=>m.FirstName)</div>
    <br/>
    <div class="label">Last Name</div>
    <div class="input-block-level">@Html.TextBoxFor(m=>m.LastName)@Html.ValidationMessageFor(m=>m.LastName)</div>
    <button type="submit" class="btn-primary">Submit</button>
}

Answer

Zabavsky picture Zabavsky · Jan 14, 2014

You can use When/Unless condition:

RuleFor(m => m.FirstName).NotEmpty().When(m => string.IsNullOrEmpty(m.LastName));
RuleFor(m => m.LastName).NotEmpty().When(m => string.IsNullOrEmpty(m.FirstName));

or

RuleFor(m => m.FirstName).NotEmpty().Unless(m => !string.IsNullOrEmpty(m.LastName));
RuleFor(m => m.LastName).NotEmpty().Unless(m => !string.IsNullOrEmpty(m.FirstName));

As for your second question, FluentValidation works with client-side validation, but not all rules are supported. Here you can find validators, that are supported on the client-side:

  1. NotNull/NotEmpty
  2. Matches (regex)
  3. InclusiveBetween (range)
  4. CreditCard
  5. Email
  6. EqualTo (cross-property equality comparison)
  7. Length

For rules that are not in the list you have to write your own FluentValidationPropertyValidator and implement GetClientValidationRules. You can find a few samples of this on the StackOverflow by doing simple search.