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 8686355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:49:36+00:00 2026-06-12T22:49:36+00:00

I am trying to write a custom validator in a Spring MVC application. I

  • 0

I am trying to write a custom validator in a Spring MVC application. I would like to know if there a way to pass the @Model object to a custom spring validator?

Say, I have a Person object and Account object. I have to write a custom validator to validate Person, but the validation is dependent of the Account object and other objects in session.

For example, Person cannot have more than 3 accounts, account types have to be of specific category and not old than 3 years (this value, ie the number years is dynamic based on the profile logged in and is in session).

How can I pass both objects, especially @Model to the validator.

public class ServiceValidator implements Validator {

@Autowired
private ServiceDAO servicesDao;

@Override
public boolean supports(Class<?> clasz) {
    return clasz.isAssignableFrom(Person.class);
}

@Override
public void validate(Object obj, Errors errors) {
     Person subscriber = (Person) obj;
    // How can I access @Model object here ???

}
  • 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-12T22:49:38+00:00Added an answer on June 12, 2026 at 10:49 pm

    Doubt if you can but have two workarounds:

    a. If it is persisted data that you are looking for, probably it is just better to retrieve it once more in the validator and validate using that data, so for eg, in your case if you are validating person and persons account details are retrievable from DB, then get it from DB and validate in your validator using the retrieved data.

    b. Probably this is a better approach if the number of places where you need to use the validator is fairly confined:

    public class ServiceValidator {
    
    @Autowired
    private ServiceDAO servicesDao;
    
    public void validate(Person subscriber, List<Account> accounts, ..., Errors errors) {
    
    
    }
    

    Just call the above validator directly from your requestmapped methods..

    In your controller..

    List<Account> accounts = //retrieve from session
    serviceValidator.validate(subscriber, accounts, ...errors);
    if (errors.hasErrors())..
    else..
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write custom validator like i`m trying below ActiveRecord::Base.class_eval do def
I'm trying to write a custom servlet (for AJAX/JSON) in which I would like
I'd like to know if I can write a validation that would validate the
I'm trying to write a custom validator to check that the 'Values' added to
Trying to figure out if there's a more succinct or elegant way to write
I'm trying to write a custom ASP.NET field validator which makes sure the input
I'm trying to write custom JFrame and JPanel for my Java application. Currently, I
I am trying to write a custom JSON deserializer in Spring. I want to
I'm trying to write a custom powershell script that will create a local user
I am trying to write a custom magento module and i've got it all

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.