Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Un campo de casilla de verificación almacena una cadena como su valor y una cadena como su texto. Su valor es 'TRUE' o 'FALSE', y su texto es 'true' o 'false'.
El constructor de la casilla de verificación toma un valor opcional y un validador opcional. El valor opcional debe ser 'TRUE', 'FALSE' o un valor booleano; de lo contrario, se establecerá de forma predeterminada en false.
Serialización
JSON
El JSON de un campo de casilla de verificación se ve de la siguiente manera:
{"fields":{"FIELDNAME":true}}
En el que FIELDNAME es una cadena que hace referencia a un campo de casilla de verificación y el valor es el valor que se aplicará al campo. El valor debe ser booleano.
XML
El XML de un campo de casilla de verificación se ve de la siguiente manera:
<fieldname="FIELDNAME">TRUE</field>
o
<fieldname="FIELDNAME">true</field>
En el que el atributo name contiene una cadena que hace referencia a un campo de casilla de verificación y el texto interno es el valor que se aplicará al campo. El valor de texto interno sigue las mismas reglas que el valor del constructor.
Ten en cuenta que, después de la deserialización y la reserialización, todos los valores de texto interno estarán en mayúsculas ('TRUE' o 'FALSE'). Esto a veces es importante cuando se comparan espacios de trabajo.
Personalización
Carácter de marca de verificación
La propiedad Blockly.FieldCheckbox.CHECK_CHAR se puede usar para cambiar el aspecto de la marca de verificación. El valor debe ser una cadena que contenga un carácter Unicode.
El valor predeterminado de la propiedad CHECK_CHAR es " \u2713" o ✓.
Esta es una propiedad global, por lo que modificará todos los campos de casilla de verificación cuando se establezca.
Cómo crear un validador de casilla de verificación
El valor de un campo de casilla de verificación es 'TRUE' o 'FALSE', por lo que un validador debe aceptar esos valores (es decir, una cadena) y mostrar 'TRUE', 'FALSE', null o undefined.
Este es un ejemplo de un validador que oculta o muestra un campo de entrada de texto según si la casilla de verificación está marcada:
[null,null,["Última actualización: 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"]]