Hi,
Please, accept my apologies because english is not my mother language.
I have a web application with Spring 3.2.1 (core, web, mvc)
All was working fine until i tried to create a form with a checkbox mapped on a Boolean value.
If i check the box, no problem.
But if i don't, then i got the following error :
I think its because the field is not submitted (when uncheck) but since i use spring form taglib, i know that there is a hidden field that should reflect the value, so Spring should be able to get a value.Code:org.springframework.beans.NotReadablePropertyException: Invalid property 'charteAccepted' of bean class [com.teamIT.common.dto.utilisateur.UserDTO]: Bean property 'charteAccepted' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:707) at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:699) at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99) at org.springframework.validation.AbstractBindingResult.rejectValue(AbstractBindingResult.java:105) at org.springframework.validation.AbstractErrors.rejectValue(AbstractErrors.java:118) at com.teamIT.web.validator.SignupValidator.validate(SignupValidator.java:54) at com.teamIT.web.controller.authentification.SignupController.signup(SignupController.java:61) [...]
So, after many search, i don't understand what is wrong in my code.
Hope you can help me.
There is my code (relevant extract):
jsp file :
Note that for some reasons, the form is submit by Javascript doing something likeCode:<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <form:form id="signupForm" action="/signup.do" method="post" modelAttribute="user"> [...] <form:checkbox id="chart" path="chartAccepted" /> [...]My Controller :Code:document.forms["signupForm"].submit();
my model (DTO) :Code:@Autowired private SignupValidator signupValidator; @Autowired private IUserService userService; @Autowired private PasswordEncoder passwordEncoder; @InitBinder public void binder(Locale locale, WebDataBinder binder) { binder.registerCustomEditor(Date.class, new DateEditor(locale)); } @RequestMapping(method = RequestMethod.GET) public String signup(@ModelAttribute("user") UserDTO user) { return "public/signup"; } @RequestMapping(method = RequestMethod.POST) public String signup(@Valid @ModelAttribute("user") UserDTO user, BindingResult result, RedirectAttributes redirectAttributes) { //Additional validator (using both JSR-303 and spring Validator) signupValidator.validate(user, result); if (result.hasErrors()) { return "public/signup"; } String encodedPassword = passwordEncoder.encode(user.getPassword()); user.setPassword(encodedPassword); user.setActive(true); userService.saveNewUser(user); redirectAttributes.addFlashAttribute("message", "You have successfully signed up and logged in."); return "redirect:/"; }
And my validate methde from my validator class :Code:public class UserDTO extends AbstractDto { @NotNull(message = "{champ.obligatoire}") private Boolean chartAccepted; /** * @return the chartAccepted */ public Boolean getChartAccepted() { return chartAccepted; } /** * @param chartAccepted * the chartAccepted to set */ public void setChartAccepted(Boolean chartAccepted) { this.chartAccepted = chartAccepted; } }
So, what is wrong ?Code:@Override public void validate(Object target, Errors errors) { if (target instanceof UserDTO) { UserDTO userDTO = (UserDTO) target; if (!userDTO.getChartAccepted()) { errors.rejectValue("charteAccepted", "error.signupForm.chartNotAccept"); } String email = userDTO.getEmail(); if (userService.findUserByEmail(email) != null) { errors.rejectValue("email", "error.signupForm.email.alreadyExist"); } } }
Thanks


Reply With Quote