title: Changing validation based on type using FluentValidation
body: <p>I have a simply model like so:</p>
  
  <pre><code>[Validator(typeof(EntryModelValidator))]
  public class EntryModel : BaseNopEntityModel
  {
    public virtual string ProductActionValue { get; set; }
  }
  </code></pre>
  
  <p>I'm using FluentValidation to validate the saving of the model. The issue is that when the user saves the values on the form, in certain situations ProductActionValue needs to be saved as an int (it will of course always be saved as a string, but it needs to be parseable as an int).</p>
  
  <p>I have the following Validation rule which ensures the value is not empty:</p>
  
  <pre><code> RuleFor(x =&gt; x.ProductCriteriaValue)
              .NotEmpty()
              .WithMessage(localizationService.GetResource("Common.FieldRequired"));
  </code></pre>
  
  <p>I tried adding the following rule to validate as an int:</p>
  
  <pre><code> RuleFor(x =&gt; Int32.Parse(x.ProductCriteriaValue))
              .GreaterThanOrEqualTo(1)
              .When(x =&gt; (ProductCriteriaTypes)x.ProductCriteriaTypeId == ProductCriteriaTypes.ProductCreatedGreaterThanXDays || (ProductCriteriaTypes)x.ProductCriteriaTypeId == ProductCriteriaTypes.ProductCreatedLessThanXDays)
              .WithMessage(localizationService.GetResource("Common.FieldRequired"));
  </code></pre>
  
  <p>But this just throws FluentValidation runtime errors. Is there anyway to acheive this?</p>
  
  <p>Thanks in advance
  Al</p>
  
  <p>UPDATED TO REFLECT AHMAD'S SOLUTION:</p>
  
  <pre><code>   {
          RuleFor(x =&gt; x.ProductCriteriaValue)
              .Must(BeANumber)
              .WithMessage(localizationService.GetResource("Common.FieldRequired"));
     }
  
      private bool BeANumber(string value)
      {
          int result;
          if (Int32.TryParse(value, out result))
          {
              return result &gt;= 1;
          }
          return false;
      }
  </code></pre>