Announcement Announcement Module
Collapse
No announcement yet.
@Valid Not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Valid Not working

    Hi,

    I am using @Valid for learning And when using @Valid in controller it's not working (Validation not working)

    My form
    PHP Code:
    import javax.validation.constraints.Size;

    public class 
    CommentForm {

        @
    Size(min=4,message="Error")
        private 
    String commend;


        public 
    String getCommend() {
            return 
    commend;
        }

        public 
    void setCommend(String commend) {
            
    this.commend commend;
        }

    My controller
    PHP Code:
     @RequestMapping(method RequestMethod.POSTvalue "/commentsForm")
        public 
    String saveCommends(CommentForm commentFormBindingResult result) {

            if (
    result.hasErrors()) {
                return 
    "commentsForms";
            }

            return return 
    "commentsForms";;
        } 
    When submitting form with empty value following code in controller not working. Please help me to find out the pblm.

    PHP Code:
            if (result.hasErrors()) {
                return 
    "commentsForms";
            } 

  • #2
    I think you still need to invoke the validator manually (this is what i have done so there could be other ways to do this)

    First you need to define a validator bean in your spring configs

    Code:
    <bean id="validator" class="org.springmodules.validation.bean.BeanValidator"  
     p:configurationLoader-ref="configurationLoader"/>
    then you need to autowire the Validator in your controller
    Code:
    @Autowired  	
    private Validator validator;  
    
     public void setValidator(Validator validator) 
    {         this.validator = validator;  	   }
    then manually invoke it in your controller
    Code:
    @RequestMapping(method = RequestMethod.POST, value = "/commentsForm") 
        public String saveCommends(CommentForm commentForm, BindingResult result) { 
    validator.validate(commentForm, result); 
            if (result.hasErrors()) { 
                return "commentsForms"; 
            } 
    
            return return "commentsForms";; 
        }

    Comment

    Working...
    X