[Logo]
Forums Register Login
BindingResult always false Spring MVC
Hello,

       I'm trying to implement spring validation framework in my mvc application. My login page has user name and password which are mandatory. I defined them as not null and not empty in the model class. If they are left blank I expect spring to bind the errors to BindingResult object. I'm not sure where I'm doing it wrong.. the hasError() method of BindingResult always return false in my controller class.

Below is my login controller



Below is my model class...



My spring xml has these necessary entries...



Below is my login jsp page...



The bindingResult.hasErrors() method in my controller class always return false. Can you please let me know, what am I missing?
What does the console print when this line is executed in your controller: 
I took your code and built this app.  I can't believe you actually got it to the validation stage, because I had to fix some pieces prior to getting it to run.
First, needed to be changed to , because doesn't exist in your controller.
Second, the tag doesn't appear to support 'required' or 'autofocus', so I got rid of them.
I commented-out the call to authenticationService to simplify testing.
Once I made the above changes, the @Validation worked properly for hibernate validating username and password fields.


 

Tim Nachreiner wrote:
Second, the tag doesn't appear to support 'required' or 'autofocus', so I got rid of them.



The form tags allow you to stick in any arbitrary attributes and reflects them in the output tag.
So "required" and "autofocus"should just get passed through to the final HTML tag.
When I run this code with "autofocus"....



I get this error



It sounds like you are saying I shouldn't be getting this error.  Can you clarify that point?
I don't get the error if I remove "autofocus".
Ah, I think it actually needs to be:


I think the same applies to the other "flag" attributes.
You are correct.
I added and it worked flawlessly.
Seems like a 'limited inheritance' feature.
Hi Tim,

Thank you for the corrections. I made the changes you suggest, and for spring forms, I modified the required and autofocus attributes as follows..





The method still returns false when I leave username blank. However, when I implement spring's validator interface and bind the results it is working fine. I'm not sure what am I missing here.
I simplified your controller class to this:  It works fine for me.  Strip your code down to this level, test, build back up.

Hi Tim,

I just copied your controller code, built and restarted the app. It is directly going to welcome page even if I leave username blank. There is certainly something else is going on. Not sure, what it is.. but for now, I have to go with implementing validator interface. Please let me know, if anything else strikes your mind. Thank you for all your time.
Let's verify you have the proper imports in your model class.  Do you have this:



Also, let's modify your sysout to look like this:



What does this sysout print when you don't enter username and password?
Yes, I do have those imports. one from javax and another from hibernate. I removed the sysout that prints username and password.

Just a side note, I wrote the custom validator class and now the validation is working fine. But I'm curious, why the annotated way is not working.
Can you post your new validation code?  I'd like to see it.
Hi Tim,

Here is my validator class..



Below is my new controller class..



Below is my modified jsp..

Thanks.  If you ever go back and figure out your original problem, please post it to this thread.
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss


This thread has been viewed 685 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jul 17, 2018 05:44:34.