# checkBox

## Purpose

Creates a checkbox form field. All the usual HTML elements apply.

## Examples

    <g:checkBox name="myCheckbox" value="${true}" />

## Description

Attributes

- `name` - The name of the checkbox
- `value` (optional) - The value of the checkbox
- `checked` (optional) - Expression if evaluates to `true` sets to checkbox to checked

The `checkBox` tag typically only requires the `name` and `value` attributes and will infer whether the checkbox should be checked or not from the value. However, if you need to override this behaviour the `checked` attribute can be used for fine grained control.

## Source

Show Source

    def checkBox = {attrs ->
            attrs.id = attrs.id ? attrs.id : attrs.name
            def value = attrs.remove('value')
            def name = attrs.remove('name')
            def disabled = attrs.remove('disabled')
            if (disabled && Boolean.valueOf(disabled)) {
                attrs.disabled = 'disabled'
            }        // Deal with the "checked" attribute. If it doesn't exist, we
            // default to a value of "true", otherwise we use Groovy Truth
            // to determine whether the HTML attribute should be displayed or not.
            def checked = true
            if (attrs.containsKey('checked')) {
                checked = attrs.remove('checked')
            }        if (checked instanceof String) checked = Boolean.valueOf(checked)        if (value == null) value = false        out << "<input type=\"hidden\" name=\"_${name}\" /><input type=\"checkbox\" name=\"${name}\" "        if (value && checked) {
                out << 'checked="checked" '
            }
            def outputValue = !(value instanceof Boolean || value?.class == boolean.class)
            if (outputValue) {
                out << "value=\"${value}\" "
            }
            // process remaining attributes
            outputAttributes(attrs)        // close the tag, with no body
            out << ' />'
        }