fh76
fh76

Reputation: 293

Binding checkbox in Thymeleaf + Spring MVC

My Spring MVC app is based on Spring boot 1.2.8, Thymeleaf, Hibernate and Hateos. I've an entity "Market" with a field "enabled" of type Boolean.

@Entity
@Table(name = "market")
public class Market {
.....
private Boolean enabled;
....
public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }
}

Code in controller for "/create"

@RequestMapping(value = "/create", method = RequestMethod.GET)
public ModelAndView create() {
    return new ModelAndView("market/create")
            .addObject("list", linkTo(methodOn(MarketController.class).list())
                    .withRel("List"))
            .addObject("market", new Market())
            .addObject("postLink",
                    linkTo(methodOn(MarketController.class).save(null, null, null, null))
                            .withRel("Save"));
}

Template "market/create", ref. http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#checkbox-fields

    <form th:action="${postLink.href}" th:object="${market}" method="post">
        ....
        <div class="form-group">
            <label th:for="${#ids.next('enabled')}" th:text="#{market.enabled}">Enabled</label>
            <input type="checkbox" th:field="*{enabled}" />
        </div>
        ....
    </form>

When opening /markets/create in the browser, getting the following exception on the line with the checkbox

Cause: org.thymeleaf.exceptions.TemplateProcessingException Attribute "value" is required in "input(checkbox)" tags when binding to non-boolean values 

Why is Thymeleaf considering the field "enabled" as non-boolean type? I have tried all my best to figure out the cause but in vain. Plz give some hints to solve it. Thanks.

Upvotes: 1

Views: 13703

Answers (4)

Himanshu
Himanshu

Reputation: 1

<input type="checkbox" th:field="*{enabled}" value="true" />

When we checked the checkbox input, it set the value of that checkbox "true". This work perfectly fine in my case.

Upvotes: 0

Dev
Dev

Reputation: 99

Controller

@Controller
public class BaseController {

    @GetMapping("/")
    private String index(DemoDto demoDto){
        return "index";
    }

    @PostMapping("/")
    private String receiveValues(DemoDto demoDto) {
        System.out.println(demoDto);
        return "index";
    }

}

DTO

public class DemoDto {
    private String name;
    private boolean global;

    //getter setter for name

    public boolean isGlobal() {
        return global;
    }
    public void setGlobal(boolean global) {
        this.global = global;
    }

    //toString()
}

HTML

<body>
    <form th:action="@{/}" th:method="post" th:object="${demoDto}">
        <label>Enter Name:</label> 
            <input type="text" th:field="*{name}" name="name"> 
        <br/>
        <label>Global</label>
            <input type="checkbox" th:field="${demoDto.global}"/>
        <input type="submit" value="Submit">
    </form>

</body>

Here most important is how you define th:field="${demoDto.global}". Both $ as well as object name demoDto are required here.

Generated html code will be.

<body>
    <form action="/" method="post">
        <label>Enter Name:</label> 
            <input type="text" name="name" id="name" value=""> 
        <br/>
        <label>Global</label>
            <input type="checkbox" id="global1" name="global" value="true"/>
            <input type="hidden" name="_global" value="on"/>
        <input type="submit" value="Submit">
    </form>

</body>

When submitted from ui received:

DemoDto [name=Dev, global=true]

Upvotes: 4

Janome314
Janome314

Reputation: 36

Try naming your attribute something other than 'enabled', perhaps 'marketEnabled'.

Upvotes: 0

David H.
David H.

Reputation: 963

In any case, the attribute valueis mandatory.

Try something like this : <input type="checkbox" th:field="*{enabled}" value="true" />. The enabled field should be setted by true when you checked the input; nullotherwise.

Upvotes: 1

Related Questions