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

  • SEARCH
  • Home
  • 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 7551769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:34:54+00:00 2026-05-30T10:34:54+00:00

I have model: [Validator(typeof(RegisterValidator))] public class RegisterModel { public string Name { get; set;

  • 0

I have model:

[Validator(typeof(RegisterValidator))]
public class RegisterModel
{
    public string Name { get; set; }

    public string Email { get; set; }

    public string Password { get; set; }

    public string ListOfCategoriess { get; set; }
}

And validator for model:

public class RegisterValidator:AbstractValidator<RegisterModel>
{
    public RegisterValidator(IUserService userService)
    {
        RuleFor(x => x.Name).NotEmpty().WithMessage("User name is required.");
        RuleFor(x => x.Email).NotEmpty().WithMessage("Email is required.");
        RuleFor(x => x.Email).EmailAddress().WithMessage("Invalid email format.");
        RuleFor(x => x.Password).NotEmpty().WithMessage("Password is required.");
        RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("Please confirm your password.");
    }
}

I have validator factory, that should resolve dependency:

public class WindsorValidatorFactory : ValidatorFactoryBase 
{
    private readonly IKernel kernel;

    public WindsorValidatorFactory(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public override IValidator CreateInstance(Type validatorType)
    {
        if (validatorType == null)
            throw new Exception("Validator type not found.");
        return (IValidator) kernel.Resolve(validatorType);
    }
}

I have IUserService, that has methods IsUsernameUnique(string name) and IsEmailUnique(string email)` and want to use it in my validator class (model should be valid only if it have unique username and email).

  1. how to use my service for validation?
  2. is it possible to register multiple Regular Expression Rules with different error messages? will it work on client side? (if no, how to create custom validation logic for it?)
  3. is validation on server side will work automatically before model pass in action method, and it is enough to call ModelState.IsValid property, or I need to do something more?
    UPDATE
  4. is it possible to access to all properties of model when validate some property? (for example I want to compare Password and ConfirmPassword when register)
  • 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-05-30T10:34:55+00:00Added an answer on May 30, 2026 at 10:34 am

    1) how to use my service for validation?

    You could use the Must rule:

    RuleFor(x => x.Email)
        .NotEmpty()
        .WithMessage("Email is required.")
        .EmailAddress()
        .WithMessage("Invalid email format.")
        .Must(userService.IsEmailUnique)
        .WithMessage("Email already taken");
    

    2) is it possible to register multiple Regular Expression Rules with different error messages? will it work on client side? (if no, how to create custom validation logic for it?)

    No, you can have only one validation type per property

    if no, how to create custom validation logic for it?

    You could use the Must rule:

    RuleFor(x => x.Password)
        .Must(password => SomeMethodContainingCustomLogicThatMustReturnBoolean(password))
        .WithMessage("Sorry password didn't satisfy the custom logic");
    

    3) is validation on server side will work automatically before model pass in action method, and it is enough to call ModelState.IsValid property, or I need to do something more?

    Yes, absolutely. Your controller action could look like this:

    [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (!ModelState.IsValid)
        {
            // validation failed => redisplay the view so that the user
            // can fix his errors
            return View(model);
        }
    
        // at this stage the model is valid => process it
        ...
        return RedirectToAction("Success");
    }
    

    UPDATE:

    4) is it possible to access to all properties of model when validate some property? (for example I want to compare Password and ConfirmPassword when register)

    Yes, of course:

    RuleFor(x => x.ConfirmPassword)
        .Equal(x => x.Password)
        .WithMessage("Passwords do not match");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Entity set and i create partial class [Bind(Include = ID,Note,Spec,Retired)] [MetadataType(typeof(SomeClass))] public
Ok say I have the following model: class Country < ActiveRecord::Base validates_presence_of :name validates_presence_of
I have a model like this: public PurchaseOrder { [Required] [StringLength(15)] public virtual string
The scenario Suppose I have the following two model classes: public class ProductColor {
I have the following viewmodel: public class ViewModel { [Display(Name = firstname, ResourceType =
Let's say I have a user class setup with validation like this: [MetadataType(typeof(ssUserMetaData))] public
I have a Model similar to the following public class TaskModel { public int
Imagine you have two fields in your Model: public class MyModel { [Required(ErrorMessageResourceType =
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
when we have validate_presence_of :name in the model and then when we put in

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.