2017-06-03 73 views
-1

我試着用required =「true」和th:required =「required」並僅使用required(因爲我使用thymeleaf而拋出一個錯誤),添加了空值的值=「」,但仍然無法正常工作。我試着爲第二個選項標記添加value =「dumb」和value =「」,同樣的...不起作用,並嘗試使用bootstrap validator.When提交表單I有一個彈出窗口,如果我按下確定,表單提交。 我使用鉻和我有另一種形式,我使用輸入+必需的,那裏它的工作。 任何幫助將不勝感激。謝謝!帶標記選擇的必需屬性不起作用

<form th:action="@{/appointment/create}" method="post" id="appointmentForm"> 
<div class="form-group"> 
       <label for="location">Alege institutia:</label> 

       <select class="form-control" required="required" 
       th:value="${appointment.institutie}"name="institutie" 
       id="institutie"> 

       <option value="" disabled="disabled" selected="selected" > 
       -- alege institutia --</option> 
       <option th:each="institutie : ${institutieList}" 
       th:text="${institutie.numeInstitutie}" ></option> 

       </select> 
</div></form> 


      var confirm = function() { 
      bootbox.confirm({ 
       title : "Confirmare programare", 
       message : "Doriti sa creeati programarea?", 
       buttons : { 
        cancel : { 
         label : '<i class="fa fa-times"></i> Nu' 
        }, 
        confirm : { 
         label : '<i class="fa fa-check"></i> Da' 
        } 
       }, 
       callback : function(result) { 
        if (result == true) { 
         $('#appointmentForm').submit(); 
        } else { 
         console.log("Confirmare anulata."); 
        } 
       } 
      }); 
     }; 

UPDATE 它負責填充 「institutieList」

@RequestMapping(value="/create", method=RequestMethod.GET) 
public String createAppointmentPost(Model model, @ModelAttribute("city") 
City city, @ModelAttribute("agency") Agency agency, Principal principal){ 

    User user = userService.findByUsername(principal.getName()); 
    Appointment appointment=new Appointment(); 
    model.addAttribute("appointment", appointment); 
    model.addAttribute("dateString", ""); 
    model.addAttribute("cities", cityService.findAll());  
    model.addAttribute("institutieList", instituteService.findAll()); 
    model.addAttribute("myFiles", userService.listAllUploadedFiles(user)); 
    return "appointment"; 
} 

更新2控制器: 問題是與提交按鈕,我已經忘了類型= 「提交」。

+1

您設置的第一個選項所選擇=「選擇」。然後你的價值是必需的,但它已經設置爲這個值。爲什麼你的第一個選項被禁用和選擇? –

+0

已禁用,因爲我不想選擇它並選中,因爲我想先顯示它。這就像第一個選項是「選擇」。如果我不使用選中,下拉菜單將顯示來自數據庫的第一個值。 – agata

+0

好吧,我嘗試了這種(沒有Thymeleaf)和它的正常工作。如果這個值沒有設置,我不能提交表格(我不熟悉Thymeleaf)。 –

回答

-1

所需的正確用法是不需要=「真」,但只需要像這樣:

<select class="form-control" required 
      th:value="${appointment.institutie}"name="institutie" 
      id="institutie"> 
+0

爲什麼downvote,如果我可能會問?請參閱https://www.w3schools.com/tags/att_select_required.asp – N4zroth

+0

據我所知,使用Thymeleaf是必需的=「必需的」。如果我按照你的建議使用需求,會拋出一個錯誤。 – agata

+0

什麼錯誤?使用required =「required」不會對Thymeleaf做任何事情,因爲它不會以th:開頭。我在我的表單中使用了簡單的要求,並且適用於我。 – N4zroth

相關問題