我在Thymeleaf中有下拉布尔选择框和3个选项。真假和空。获得e

时间:2016-12-22 06:50:00

标签: javascript spring spring-boot thymeleaf

我有一个下拉表单,其中包含三个选项,这是POST FORM的一部分。 TRUE,FALSE和其他东西(尝试使用null,0,只是字符串)是三个选项。现在,当我提交这样的表格时,一切正常。

  <select class="form-control" th:field="*{activated}" >
                            <option id="choose" value="null">CHOOSE_VALUE</option>
                            <option th:each="state : ${T(com.company.model.User.State).values()}" id="activated" data-th-field="*{activated}"   th:value="${state}" th:text="${state}"></option>
                        </select>

但是当我尝试发布时,控制器会捕获这样的错误并将其发送回视图。

   if(!uniqueUsername) {
        ObjectError objectError = new ObjectError("ERROR", "ERROR! Username already exists!");
        bindingResult.addError(objectError);
        logger.error(MessageFormat.format("Admin {0} could not create user {1}, because Username already exists",user.getEditor(), user));
        return "user-create";
    }

然后我得到一个错误说&#34; null&#34;不是布尔值或&#34; CHOOSE_VALUE&#34;不是布尔值。

我想要的是三种选择。第一个必须没有任何意义,你不能在不改变的情况下提交表格。然后javascript将告诉您选择该表单。但每次我尝试添加其他空选项,如布尔值的NULL值,然后我得到一个错误。如果有任何其他字段错误,百里香叶本身捕获的字段太短,则不会显示错误页面。

1 个答案:

答案 0 :(得分:0)

 <select class="selectpicker" title="Choose value" th:field="*{activated}" >
                            <option th:each="state : ${T(com.company.model.User.State).values()}" id="activated" data-th-field="*{activated}"   th:value="${state}" th:text="${state}"></option>
                        </select>

用过这个: https://silviomoreto.github.io/bootstrap-select/