lundi 19 octobre 2015

Spring Form submit not passing checkbox unchecked value when enctype="multipart/form-data"

I am using Spring and to send checkbox value to controller. It was working fine until I added enctype="multipart/form-data" to . Now when I un-check the checkbox, I am getting an error saying "byte value mapped to the checkbox can not be Null". What am I missing?

jsp:

<form:form modelAttribute="ad" method="post" class="form-horizontal" autocomplete="off" id="add-ad-form" 
    action="?${_csrf.parameterName}=${_csrf.token}" enctype="multipart/form-data">
            <div class="form-group">
        <label for="adContact.phoneText" class="col-sm-2 control-label">Text Me</label>
        <div class="col-sm-5">
          <form:checkbox path="adContact.phoneText" value="1" />
        </div>
    </div>

Spring generates the html as:

      <input id="adContact.notifyMe1" name="adContact.notifyMe" value="1" checked="checked" type="checkbox">
      <input name="_adContact.notifyMe" value="on" type="hidden">

controller:

@RequestMapping(value = "/ad/adAdd3/{categoryId}", method = RequestMethod.POST)
public String postAdAdd(@RequestParam("categoryId") int categoryID,
        @ModelAttribute("ad") @Valid Ad ad, BindingResult aaResult,
        RedirectAttributes attr, SessionStatus aaStatus,
        Principal aaPrincipal) {

    if (aaResult.hasErrors()) {
        attr.addFlashAttribute("org.springframework.validation.BindingResult.ad", aaResult);
        attr.addFlashAttribute("ad", ad);
        return "redirect:/ad/adAdd3/" + categoryID;
    } else {
        ad = this.caService.saveAd(ad, categoryID,
                ((UserDetailsImpl) ((Authentication) aaPrincipal)
                        .getPrincipal()).getUser());

        aaStatus.setComplete();
        return "redirect:/ad/adDetail/" + ad.getId();
    }
}




Aucun commentaire:

Enregistrer un commentaire