• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Use of Binding result in Controller for particular input

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to validate an input parameter in the Controller with my custom validator and then use BindingResult to get validation results.
So I made ConstraintValidator and @interface "ValidatorCust" to call my own check. It runs like a charm with the model attributes. So its very convenient and concise to check errors with just binding result:



But now I'm stuck with how to check an input parameter in controller.

The check runs fine and my restriction works right, but in further its not works as expected: I'm getting javax.validation.ConstraintViolationException with correct check result and then applications stops without any ability to just check Binding result.


After lots of reading I figured out that I can use @ExceptionHandler to override this, but it is verbose and no so convenient comparing to BindingResult's check. Another way I guess is just instantiate ValidatorFactory, Validator and then explicitly do check inside a Controller's code (as I take, explicit using of Spring's validator here is almost the same).

The question: is there any way to use Binding Result with input parameter validation (without sticking to model)? And if its not, which way for me is the best?
 
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was taught that the bindingResult parameter should always follow the parameter annotated with @Valid.  Might want to swap the order of your last two parameters.
 
George Smithss
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Nachreiner wrote:I was taught that the bindingResult parameter should always follow the parameter annotated with @Valid.  Might want to swap the order of your last two parameters.


Thank you, but it's not working

Still so I'm unable to use bindingResult.
 
Bartender
Posts: 2264
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, George,
I guess your input User has a duplicated ID that violates the DB constraint?
 
George Smithss
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:Hi, George,
I guess your input User has a duplicated ID that violates the DB constraint?


Hi. Thanks for your comment. I've checked uniqueness of ID attentively and there is no violation of DB constraint.
 
Himai Minh
Bartender
Posts: 2264
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, George,
Is any attribute in the User violating the any constraints?
 
reply
    Bookmark Topic Watch Topic
  • New Topic