Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Use of Binding result in Controller for particular input

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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: 89
1
  • Mark post as helpful
  • send pies
  • 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: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Ranch Hand
Posts: 1871
12
  • Mark post as helpful
  • send pies
  • 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: 9
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1871
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, George,
Is any attribute in the User violating the any constraints?
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic