Konstruktor checkbox przyjmuje opcjonalną wartość i opcjonalny walidator. Opcjonalna wartość powinna być wartością 'TRUE', 'FALSE' lub logiczną, w przeciwnym razie zostanie domyślnie ustawiona wartość false.
Publikacja w odcinkach
JSON
Kod JSON pola z polem wyboru wygląda tak:
{"fields":{"FIELDNAME":true}}
Gdzie FIELDNAME to ciąg znaków odwołujący się do pola z polem wyboru, a wartość to wartość do zastosowania w polu. Wartość musi być typu logicznego.
XML
Kod XML pola wyboru wygląda tak:
<fieldname="FIELDNAME">TRUE</field>
lub
<fieldname="FIELDNAME">true</field>
Atrybut name zawiera ciąg znaków odwołujący się do pola wyboru, a tekst wewnętrzny to wartość, którą należy zastosować do pola. Wartość tekstu wewnętrznego podlega tym samym regułom co wartość konstruktora.
Pamiętaj, że po deserializacji i ponownie serializacji wszystkie wartości tekstu wewnętrznego będą zapisane wielkimi literami ('TRUE' lub 'FALSE'). Czasami jest to ważne podczas porównywania obszarów roboczych.
Dostosowywanie
Znak zaznaczenia
Właściwości Blockly.FieldCheckbox.CHECK_CHAR można używać do zmiany wyglądu znacznika wyboru. Wartość powinna być ciągiem znaków zawierającym znak Unicode.
Właściwość CHECK_CHAR ma domyślnie wartość „\u2713” lub ✓.
Jest to właściwość globalna, więc po jej ustawieniu zostanie ona zastosowana do wszystkich pól wyboru.
Tworzenie walidatora pola wyboru
Wartość pola z polem wyboru to 'TRUE' lub 'FALSE', więc weryfikator powinien akceptować te wartości (czyli ciąg znaków) i zwracać 'TRUE', 'FALSE', null lub undefined.
Oto przykład mechanizmu weryfikacji, który ukrywa lub wyświetla pole tekstowe w zależności od tego, czy pole wyboru jest zaznaczone:
[null,null,["Ostatnia aktualizacja: 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"]]