Announcement Announcement Module
Collapse
No announcement yet.
Problem with checkbox and model Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with checkbox and model

    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 :

    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)
    	[...]
    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.

    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 :

    Code:
    <%@ 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" />
       [...]
    Note that for some reasons, the form is submit by Javascript doing something like
    Code:
    document.forms["signupForm"].submit();
    My Controller :
    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:/";
    }
    my model (DTO) :
    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;
    }
    
    }
    And my validate methde from my validator class :
    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");
    			}
    
    		}
    
    	}
    So, what is wrong ?

    Thanks

  • #2
    Found the problem, got un extra "e" on my string :

    Code:
    if (!userDTO.getChartAccepted()) {
    				errors.rejectValue("charteAccepted",
    						"error.signupForm.chartNotAccept");
    			}
    Problem solved.

    Comment

    Working...
    X