2017-01-19 4 views
0

おそらく私は少しと愚かな何かが不足しています。しかし、私は6つのチェックボックスを持つフォームを持って、あなたは同時に最大4つのチェックボックスでチェックすることができます。これはバリデータのバリデーションメソッドの内部です。私のバリデータクラスは以下の通りです:フォームの検証が実行されていない、春の起動

@Component 
public class GraficiUserFormValidator implements Validator { 

    @Override 
    public boolean supports(Class<?> arg0) { 
     return arg0.equals(GraficiUserForm.class); 
    } 

    @Override 
    public void validate(Object arg0, Errors arg1) { 
     GraficiUserForm form = (GraficiUserForm) arg0; 
     validateCheckBoxes(arg1, form); 

    } 

    private void validateCheckBoxes(Errors arg1, GraficiUserForm form) { 
     if ((form.getG1().equals("on") && form.getG2().equals("on") && form.getG3().equals("on") && form.getG4().equals("on") && 
       form.getG5().equals("on") && form.getG6().equals("on")) ||(form.getG1().equals("on") && form.getG2().equals("on") && form.getG3().equals("on") && form.getG4().equals("on") && 
       form.getG5().equals("on"))||(form.getG1().equals("on") && form.getG2().equals("on") && form.getG3().equals("on") && form.getG4().equals("on") && 
       form.getG6().equals("on"))||(form.getG1().equals("on") && form.getG2().equals("on") && form.getG3().equals("on") && form.getG5().equals("on") && 
       form.getG6().equals("on"))||(form.getG1().equals("on") && form.getG2().equals("on") && form.getG4().equals("on") && form.getG5().equals("on") && 
       form.getG6().equals("on"))||(form.getG1().equals("on") && form.getG3().equals("on") && form.getG4().equals("on") && form.getG5().equals("on") && 
       form.getG6().equals("on"))||(form.getG2().equals("on") && form.getG3().equals("on") && form.getG4().equals("on") && form.getG5().equals("on") && 
       form.getG6().equals("on")) 
       ){ 
      arg1.reject("grafici.tooMany", "Troppi grafici selezionati, si possono selezionare solo 4 grafici"); 
     } 

    } 

} 

それから私は、コントローラにバリデータをautowired:

@Controller 
public class UsersController { 

    private static final Logger LOGGER = LoggerFactory.getLogger(UsersController.class); 
    private final UserService userService; 
    private final SitoService sitoService; 
    private final ClusterService clusterService; 
    private final GraficiUserFormValidator graficiUserFormValidator; 

    @Autowired 
    public UsersController(UserService userService, ClusterService clusterService, SitoService sitoService, GraficiUserFormValidator graficiUserFormValidator) { 
     this.userService = userService; 
     this.sitoService = sitoService; 
     this.clusterService = clusterService; 
     this.graficiUserFormValidator = graficiUserFormValidator; 
    } 

    @InitBinder("form") 
    public void initBinder(WebDataBinder binder) { 
     binder.addValidators(graficiUserFormValidator); 
    } 

と私は同じコントローラで処理されるフォームの方法で構築:

@PreAuthorize("hasAuthority('ADMIN') or hasAuthority('CLUSTERADMIN') or hasAuthority('SITEADMIN')") 
    @RequestMapping(value = "/update/settings", method = RequestMethod.POST) 
    public String updateSettingsGraphs(@Valid GraficiUserForm form, HttpSession session, HttpServletRequest request, BindingResult result, 
      Model model, Long id){ 
     SecurityContextHolderAwareRequestWrapper wrapper = new SecurityContextHolderAwareRequestWrapper(request, null); 

     if (result.hasErrors()) { 
      if (wrapper.isUserInRole("ADMIN")) { 
       result.reject("graphs.tooMany", "Puoi selezionare solo quattro grafici alla volta"); 
       Collection<User> users = userService.getAllUsers(); 
       model.addAttribute("users", users); 


       for (User user : users) { 
        model.addAttribute("form"+user.getId().toString(), new GraficiUserForm()); 
       } 

       return "users"; 
      } 

      else if (wrapper.isUserInRole("CLUSTERADMIN")) { 
       result.reject("graphs.tooMany", "Puoi selezionare solo quattro grafici alla volta"); 
       Collection<User> users = clusterService.getAllByCluster(id); 
       model.addAttribute("users", users); 


       for (User user : users) { 
        model.addAttribute("form"+user.getId().toString(), new GraficiUserForm()); 
       } 
       return "users-cluster"; 
      } 

      else { 
       result.reject("graphs.tooMany", "Puoi selezionare solo quattro grafici alla volta"); 
       Collection<User> users = sitoService.getAllBySite(id); 
       model.addAttribute("users", users); 


       for (User user : users) { 
        model.addAttribute("form"+user.getId().toString(), new GraficiUserForm()); 
       } 
       return "users-site"; 
      } 
     } 
     User user = userService.getUserById(form.getUserid()); 
     if("on".equals(form.getG1())){ 
      user.setG1("S"); 
     } else { 
      user.setG1("N"); 
     } 
     if("on".equals(form.getG2())){ 
      user.setG2("S"); 
     } else { 
      user.setG2("N"); 
     } 
     if("on".equals(form.getG3())){ 
      user.setG3("S"); 
     } else { 
      user.setG3("N"); 
     } 
     if("on".equals(form.getG4())){ 
      user.setG4("S"); 
     } else { 
      user.setG4("N"); 
     } 
     if("on".equals(form.getG5())){ 
      user.setG5("S"); 
     } else { 
      user.setG5("N"); 
     } 
     if("on".equals(form.getG6())){ 
      user.setG6("S"); 
     } else { 
      user.setG6("N"); 
     } 
     user = userService.save(user); 
     if (wrapper.isUserInRole("ADMIN")) { 
      return "redirect:/users"; 
     } 

     else if (wrapper.isUserInRole("CLUSTERADMIN")) { 
      return "redirect:/users-cluster"; 
     } 

     else { 
      return "redirect:/users-site"; 
     } 

    } 

} 

私のWebページは、FreeMarkerのことで構築され、以下のようになっている。

<#-- @ftlvariable name="users" type="java.util.List<it.energyway.application.domain.User>" --> 
<#-- @ftlvariable name="_csrf" type="org.springframework.security.web.csrf.CsrfToken" --> 
<#list users as user> 
<#-- @ftlvariable name="form${user.id}" type="it.energyway.application.domain.GraficiUserForm" --> 
</#list> 
<#import "/spring.ftl" as spring> 
<!DOCTYPE html> 
<html lang="it"> 
<head> 
    <meta charset="utf-8"> 
    <title>List of Users</title> 
</head> 
<body> 

<nav role="navigation"> 
    <ul> 
     <li><a href="/">Dashboard</a></li> 
     <li><a href="/user/create">Create a new user</a></li> 
     <li>Amministrazione 
      <ul> 
       <li><a href="/users">Gestione Utenti e Permessi</a></li> 
      </ul> 
     </li> 
    </ul> 
</nav> 

<h1>Utenti</h1> 

<table> 
    <thead> 
    <tr> 
     <th>E-mail</th> 
     <th>Role</th> 
     <th>Grafico 1</th> 
     <th>Grafico 2</th> 
     <th>Grafico 3</th> 
     <th>Grafico 4</th> 
     <th>Grafico 5</th> 
     <th>Grafico 6</th> 
     <th>Cambio</th> 
    </tr> 
    </thead> 
    <tbody> 
    <#list users as user> 
    <form role="form${user.id}" name="form${user.id}" action="/update/settings" method="post"> 
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 
    <input type="hidden" name="userid" id="userid" value="${user.id}"/> 
    <tr> 
     <td><a href="/user/${user.id}">${user.email}</a></td> 
     <td>${user.role}</td> 
     <td><input class="graphs" type="checkbox" name="g1" <#if (user.g1 == "S")>checked=true<#else></#if></td> 
     <td><input class="graphs" type="checkbox" name="g2" <#if (user.g2 == "S")>checked=true<#else></#if></td> 
     <td><input class="graphs" type="checkbox" name="g3" <#if (user.g3 == "S")>checked=true<#else></#if></td> 
     <td><input class="graphs" type="checkbox" name="g4" <#if (user.g4 == "S")>checked=true<#else></#if></td> 
     <td><input class="graphs" type="checkbox" name="g5" <#if (user.g5 == "S")>checked=true<#else></#if></td> 
     <td><input class="graphs" type="checkbox" name="g6" <#if (user.g6 == "S")>checked=true<#else></#if></td> 
     <td><button type="submit">Save</button></td> 
    </tr> 
    </form> 
    </#list> 
    </tbody> 
</table> 
<#list users as user> 
    <@spring.bind "form${user.id}" /> 
</#list> 
<#if (spring.status.error)??> 
<ul> 
    <#list spring.status.errorMessages as error> 
     <li>${error}</li> 
    </#list> 
</ul><#else> 
</#if> 
</body> 
</html> 

4つ以上のチェックボックスを選択すると、出力に何のエラーもスローされません... 誰もがなぜか分かりますか?

+1

'BindingResult'は' @ Valid'で注釈された引数の後になければなりません。あなたの場所は正しい場所ではなく、最後にあります。また、春のセキュリティでリクエストをラップする必要もありません(これはすでにSpring Securityによって行われています)。 –

答えて

1

お使いのコントローラメソッドで最初にこれを追加してみてください。

ValidationUtils.invokeValidator(userFormValidator, form, result); 

@Valid注釈は、JSR-303/JSR-349(ビーン検証API)の制約とバリデータを使用して検証をトリガーしますが、上記の指定されたあなたのバリデータがあります春バリデーター。

更新:BindingResult@Validと引数を次の、あなたが@InitBinderとバリデータを登録した場合としてM. Deinumは、@Validがあまりにも春のバリデータ(S)と連携して動作するはずです、コメントで指摘しています。 JSR-303/349アノテーションとSpringバリデーターを混在させるのは論理的ではないと思います。

+0

@ M.Deinumあなたはあまりにもそれを使うことができたのか分からなかった!更新しました。 – holmis83

関連する問題