简体   繁体   中英

How can I exclude/ disallow field in Validator in Spring

I have 2 forms

  1. Add Book (addBook.jsp) and It's controller (AddBookController.java)
  2. Edit Book(editBook.jsp) and It's controller (EditBookController.java)

Book.java is bean class for both views.

I have same UI for editBook.jsp and addBook.jsp .

I have created org.springframework.validation.Validator ( BookValidator.java ) for add book same Validator can be use for EditBookController.java

But in AddBookController when user is entering Book information user must enter unique bundle number. I have some validations for Unique bundle number on BookValidator .

This validations must not work while user EditBook.

I want to exclude / disallow field for validations while book edit.

I have done

binder.setDisallowedFields(new String[] {"bundleNumber"});

but not helped me.

Please suggest me How i can re use validator ?

It sounds like you need two validators. Perhaps the more specific one can be a subclass of the more general one. The template method pattern may help here with different, overridable methods for each piece of validation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM