Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9026621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:34:00+00:00 2026-06-16T06:34:00+00:00

I have a simply model like so: [Validator(typeof(EntryModelValidator))] public class EntryModel : BaseNopEntityModel {

  • 0

I have a simply model like so:

[Validator(typeof(EntryModelValidator))]
public class EntryModel : BaseNopEntityModel
{
  public virtual string ProductActionValue { get; set; }
}

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).

I have the following Validation rule which ensures the value is not empty:

 RuleFor(x => x.ProductCriteriaValue)
            .NotEmpty()
            .WithMessage(localizationService.GetResource("Common.FieldRequired"));

I tried adding the following rule to validate as an int:

 RuleFor(x => Int32.Parse(x.ProductCriteriaValue))
            .GreaterThanOrEqualTo(1)
            .When(x => (ProductCriteriaTypes)x.ProductCriteriaTypeId == ProductCriteriaTypes.ProductCreatedGreaterThanXDays || (ProductCriteriaTypes)x.ProductCriteriaTypeId == ProductCriteriaTypes.ProductCreatedLessThanXDays)
            .WithMessage(localizationService.GetResource("Common.FieldRequired"));

But this just throws FluentValidation runtime errors. Is there anyway to acheive this?

Thanks in advance
Al

UPDATED TO REFLECT AHMAD’S SOLUTION:

   {
        RuleFor(x => x.ProductCriteriaValue)
            .Must(BeANumber)
            .WithMessage(localizationService.GetResource("Common.FieldRequired"));
   }

    private bool BeANumber(string value)
    {
        int result;
        if (Int32.TryParse(value, out result))
        {
            return result >= 1;
        }
        return false;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:34:02+00:00Added an answer on June 16, 2026 at 6:34 am

    You can use the Predicate Validator (aka Must):

    RuleFor(x => x.ProductCriteriaValue)
        .Must(x => Int32.Parse(x.ProductCriteriaValue) >= 1)
        .When(x => (ProductCriteriaTypes)x.ProductCriteriaTypeId == ProductCriteriaTypes.ProductCreatedGreaterThanXDays || (ProductCriteriaTypes)x.ProductCriteriaTypeId == ProductCriteriaTypes.ProductCreatedLessThanXDays)
        .WithMessage(localizationService.GetResource("Common.FieldRequired"));
    

    Of course, this assumes that the parse won’t fail. Will ProductCriteriaValue always be a number and parse fine? If so, this is ok. Otherwise, you might want to check this better by using Int32.TryParse and changing the predicate as follows:

        .Must(x =>
        {
            int result;
            if (Int32.TryParse(x.ProductCriteriaValue, out result))
            {
                return result >= 1;
            }
            return false;
        })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a domain model like this: public class MyDomainEntity { public double
I have a simple model like this: class User < ActiveRecord::Base serialize :preferences end
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
Suppose I have a simple model, such as Record: @Model public class Record {
I have a model which has 2 fields, a and b like this: class
I have a simple model from simulink and I would like to generate code
I have a simple model class (Part), which pulls from it's information from a
I have simple ASP.NET MVC action like this : public ActionResult Edit(EditPostViewModel data) {
I have this code in my user model: class User < ActiveRecord::Base attr_accessible :email,
I have this domain model: class Person < ActiveRecord::Base composed_of :address, mapping: [%w(address_street street),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.