O construtor de caixas de seleção recebe um valor opcional e um
validador opcional. O valor opcional precisa ser
'TRUE', 'FALSE' ou um booleano. Caso contrário, o padrão será false.
Serialização
JSON
O JSON de um campo de caixa de seleção é assim:
{"fields":{"FIELDNAME":true}}
Em que FIELDNAME é uma string que faz referência a um campo de caixa de seleção e
o valor é o valor a ser aplicado ao campo. O valor precisa ser booleano.
XML
O XML de um campo de caixa de seleção tem esta aparência:
<fieldname="FIELDNAME">TRUE</field>
ou
<fieldname="FIELDNAME">true</field>
Onde o atributo name contém uma string que faz referência a um campo de caixa de seleção
e o texto interno é o valor a ser aplicado ao campo. O valor do texto interno
segue as mesmas regras do valor do construtor.
Depois de desserialização e reserialização, todos os valores do texto interno
vão estar em maiúsculas ('TRUE' ou 'FALSE'). Isso às vezes é importante
ao comparar espaços de trabalho.
Personalização
Marca de seleção
A propriedade Blockly.FieldCheckbox.CHECK_CHAR pode ser usada para mudar a
aparência da marca de seleção. O valor precisa ser uma string que contenha um caractere
Unicode.
O padrão da propriedade CHECK_CHAR é '\u2713' ou ✓.
Essa é uma propriedade global, então ela vai modificar todos os campos de caixa de seleção quando definida.
Como criar um validador de caixa de seleção
O valor de um campo de caixa de seleção é 'TRUE' ou 'FALSE'. Portanto, um validador precisa
aceitar esses valores (ou seja, uma string) e retornar 'TRUE', 'FALSE', null ou
undefined.
Confira um exemplo de validador que oculta ou mostra um campo de entrada de texto com base
na seleção da caixa de seleção:
[null,null,["Última atualização 2025-07-25 UTC."],[[["\u003cp\u003eCheckbox fields store a string value (\u003ccode\u003e'TRUE'\u003c/code\u003e or \u003ccode\u003e'FALSE'\u003c/code\u003e) and a corresponding text representation (\u003ccode\u003e'true'\u003c/code\u003e or \u003ccode\u003e'false'\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eCheckbox fields can be easily created and customized using JSON or JavaScript within Blockly.\u003c/p\u003e\n"],["\u003cp\u003eSerialization of checkbox fields is supported in both JSON and XML formats, ensuring data persistence.\u003c/p\u003e\n"],["\u003cp\u003eThe checkmark character in checkbox fields can be globally customized using the \u003ccode\u003eBlockly.FieldCheckbox.CHECK_CHAR\u003c/code\u003e property.\u003c/p\u003e\n"],["\u003cp\u003eValidators can be implemented for checkbox fields to control behavior based on their state, such as dynamically showing/hiding other fields.\u003c/p\u003e\n"]]],["Checkbox fields store either `'TRUE'` or `'FALSE'` as their value and `'true'` or `'false'` as text. They can be created via JSON or JavaScript. Serialization in JSON uses boolean values, while XML uses `TRUE` or `FALSE` strings. Validators can modify the field's behavior, accepting and returning `'TRUE'` or `'FALSE'`. The checkmark character is customizable globally. The `getValueBoolean` method should not be used within validators.\n"],null,["# Checkbox fields\n\nA checkbox field stores a string as its value, and a string as its text. Its\nvalue is either `'TRUE'` or `'FALSE'`, and its text is either `'true'` or\n`'false'`.\n\n#### Checkbox field\n\n!\\[[A block with the label \"checkbox:\" and a checkbox field with a check\nmark.](/static/blockly/images/fields/checkbox/on_block.png)\n\n#### Checkbox field on collapsed block\n\nCreation\n--------\n\n### JSON\n\n {\n \"type\": \"example_checkbox\",\n \"message0\": \"checkbox: %1\",\n \"args0\": [\n {\n \"type\": \"field_checkbox\",\n \"name\": \"FIELDNAME\",\n \"checked\": true\n }\n ]\n }\n\n### JavaScript\n\n Blockly.Blocks['example_checkbox'] = {\n init: function() {\n this.appendDummyInput()\n .appendField('checkbox:')\n .appendField(new Blockly.FieldCheckbox(true), 'FIELDNAME');\n }\n };\n\nThe checkbox constructor takes in an optional value and an optional\n[validator](#creating_a_checkbox_validator). The optional value should be either\n`'TRUE'`, `'FALSE'`, or a boolean, otherwise it will default to `false`.\n\nSerialization\n-------------\n\n### JSON\n\nThe JSON for a checkbox field looks like so: \n\n {\n \"fields\": {\n \"FIELDNAME\": true\n }\n }\n\nWhere `FIELDNAME` is a string referencing a checkbox field, and\nthe value is the value to apply to the field. The value must be a boolean.\n\n### XML\n\nThe XML for a checkbox field looks like so: \n\n \u003cfield name=\"FIELDNAME\"\u003eTRUE\u003c/field\u003e\n\nor \n\n \u003cfield name=\"FIELDNAME\"\u003etrue\u003c/field\u003e\n\n| **Note:** Quotes do not need to be applied to the inner text.\n\nWhere the `name` attribute contains a string referencing an checkbox field,\nand the inner text is the value to apply to the field. The inner text value\nfollows the same rules as the constructor value.\n\nNote that after being deserialized and reserialized all of the inner text\nvalues will be in caps (`'TRUE'` or `'FALSE'`). This is sometimes important\nwhen diffing workspaces.\n\nCustomization\n-------------\n\n### Checkmark character\n\nThe `Blockly.FieldCheckbox.CHECK_CHAR` property can be used to change what the\ncheckmark looks like. The value should be a string containing a unicode\ncharacter.\n\nThe `CHECK_CHAR` property defaults to '\\\\u2713' or ✓.\n\nThis is a global property, so it will modify all checkbox fields when set.\n\nCreating a checkbox validator\n-----------------------------\n\n| **Note:** For information on validators in general see [Validators](/blockly/guides/create-custom-blocks/fields/validators).\n\nA checkbox field's value is either `'TRUE'` or `'FALSE'` so a validator should\naccept those values (i.e. a string) and return `'TRUE'`, `'FALSE'`, `null`, or\n`undefined`.\n| **Caution:** the `getValueBoolean` method should not be used inside of validators, because it returns based on the current value, not the new value.\n\nHere's an example of a validator that hides or shows a text input field based on\nwhether the checkbox is checked: \n\n validate: function(newValue) {\n var sourceBlock = this.getSourceBlock();\n sourceBlock.showTextField_ = newValue == 'TRUE';\n sourceBlock.updateTextField();\n\n return newValue;\n },\n\n updateTextField: function() {\n var input = this.getInput('DUMMY');\n if (this.showTextField_ && !this.getField('TEXT')) {\n input.appendField(new Blockly.FieldTextInput(), 'TEXT');\n } else if (!this.showTextField_ && this.getField('TEXT')) {\n input.removeField('TEXT');\n }\n }\n\n#### Checkbox field with a validator"]]