title: Use FluentValidation to access the database validating more than one field
body: <p>I learned how to use the fluent validator I would like to know if you could help me with a question. I have a personal system where in my controller the create post method has the following code:</p>
  <pre class="lang-cs prettyprint-override"><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Create (TicketViewModel model)
  {
      ICollection &lt;Piece&gt; pieces;
      try
      {
          if (ModelState.IsValid)
          {
              if (_ticketRepository.GetById (model.Id)! = null)
              {
                  Console.WriteLine (&quot;Ticket already exists!&quot;);
              }
  
              var _model = _mapper.Map &lt;Ticket&gt; (model);
              var count = _ticketRepository.FindAllByPiece (_model);
  
              if (count&gt; 0)
              {
                  ModelState.AddModelError (&quot;&quot;, &quot;This ticket already exists for this room. Check the piece, date and time.&quot;);
                  pieces = _pieceRepository.FindAll ();
                  model.Pieces = pieces;
                  return View (model);
              }
  
              _ticketRepository.Insert (_model);
              model.Seats = RegistrationSeats (model.QuantityOfSeats);
  
              return RedirectToAction (&quot;Index&quot;);
          }
          pieces = _pieceRepository.FindAll ();
  
          model.Pieces = pieces;
  
          return View (model);
      }
      catch (Exception ex)
      {
          Console.WriteLine (ex.Message);
          return View (model);
      }
  }
  </code></pre>
  <p>Note that in the code above I have a part like this:</p>
  <pre class="lang-cs prettyprint-override"><code>var count = _ticketRepository.FindAllByPiece (_model);    
  if (count&gt; 0)
  {
      ModelState.AddModelError (&quot;&quot;, &quot;This ticket already exists for this room. Check the piece, date and time.&quot;);
      pieces = _pieceRepository.FindAll ();
      model.Pieces = pieces;
      return View (model);
  }
  </code></pre>
  <ul>
  <li>This code snippet calls a service from the repository that checks if the model (ticket) passed has the same partId, date and schedule as the one already in the bank. If there is a record with these same values, he does not add the ticket until the person creates a different one than the one that already exists.</li>
  <li>Here is the code of the service that accesses the bank doing this search:</li>
  </ul>
  <pre class="lang-cs prettyprint-override"><code>public int FindAllByPiece(Ticket model)
  {
      return _saleTheaterTicketsContext.Tickets.Include(x =&gt; x.Piece).Where(x =&gt; x.PieceId == model.PieceId &amp;&amp; x.Date == model.Date &amp;&amp; x.Schedule == model.Schedule).Count();
  }
  </code></pre>
  <ul>
  <li>This works well and to show in the View the message I added in <code>ModelState.AddModelError</code> I do like this in View:</li>
  </ul>
  <pre class="lang-html prettyprint-override"><code>&lt;strong class = &quot;text-danger&quot;&gt;@ Html.ValidationSummary (true)&lt;/strong&gt;
  </code></pre>
  <ul>
  <li>My question is as I am using FluentValidation, I would like to know if there is any way to do this with it, so that the controller is not full of things. I did some research, but to no avail. I tried to instantiate the repository in the Validation class (which I will put the code below) but I don't know how to do the validation for these 3 fields together, passing a model to the repository. Would you help me?</li>
  <li>Here is code from the Validation class using FluentValidator:</li>
  </ul>
  <pre class="lang-cs prettyprint-override"><code>public class TicketViewModelValidator: AbstractValidator &lt;TicketViewModel&gt;
  {
      public TicketViewModelValidator ()
      {
          RuleFor (x =&gt; x.Price)
              .NotEmpty (). WithMessage (&quot;Enter the price&quot;)
              .GreaterThan (0) .WithMessage (&quot;The price must be greater than 0.00&quot;);
          RuleFor (x =&gt; x.QuantityOfSeats)
              .NotEmpty (). WithMessage (&quot;Enter the number of seats&quot;)
              .GreaterThanOrEqualTo (10) .WithMessage (&quot;The number of seats must be at least 10&quot;);
          RuleFor (x =&gt; x.Date)
              .NotEmpty (). WithMessage (&quot;Enter Date&quot;)
              .Must (ValidDate) .WithMessage (&quot;Date must be greater than or equal to today&quot;);
          RuleFor (x =&gt; x.Schedule)
              .NotEmpty (). WithMessage (&quot;Enter time&quot;);
          RuleFor (x =&gt; x.PieceId)
              .NotEmpty (). WithMessage (&quot;Select the part&quot;);
      }
  
      public static bool ValidDate (DateTime date)
      {
          if (date.Date&gt; = DateTime.Now.Date)
          {
              return true;
          }
  
          return false;
      }
  }
  </code></pre>