Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Un champ de case à cocher stocke une chaîne comme valeur et une chaîne comme texte. Sa valeur est 'TRUE' ou 'FALSE', et son texte est 'true' ou 'false'.
Le constructeur de la case à cocher accepte une valeur facultative et un valideur facultatif. La valeur facultative doit être 'TRUE', 'FALSE' ou une valeur booléenne. Sinon, la valeur par défaut est false.
Sérialisation
JSON
Le code JSON d'un champ de case à cocher se présente comme suit:
{"fields":{"FIELDNAME":true}}
Où FIELDNAME est une chaîne faisant référence à un champ de case à cocher, et la valeur est la valeur à appliquer au champ. La valeur doit être booléenne.
XML
Le code XML d'un champ de case à cocher se présente comme suit:
<fieldname="FIELDNAME">TRUE</field>
ou
<fieldname="FIELDNAME">true</field>
Lorsque l'attribut name contient une chaîne faisant référence à un champ de case à cocher, le texte interne est la valeur à appliquer au champ. La valeur du texte interne suit les mêmes règles que la valeur du constructeur.
Notez qu'après avoir été désérialisé et sérialisé, toutes les valeurs de texte interne seront en majuscules ('TRUE' ou 'FALSE'). Cela est parfois important lors de la comparaison d'espaces de travail.
Personnalisation
Caractère coche
La propriété Blockly.FieldCheckbox.CHECK_CHAR permet de modifier l'apparence de la coche. La valeur doit être une chaîne contenant un caractère Unicode.
La propriété CHECK_CHAR est définie par défaut sur '\u2713' ou ✓.
Il s'agit d'une propriété globale. Par conséquent, elle modifie tous les champs de case à cocher lorsqu'elle est définie.
Créer un validateur de case à cocher
La valeur d'un champ de case à cocher est 'TRUE' ou 'FALSE'. Un validateur doit donc accepter ces valeurs (c'est-à-dire une chaîne) et renvoyer 'TRUE', 'FALSE', null ou undefined.
Voici un exemple de valideur qui masque ou affiche un champ de saisie de texte en fonction de la case à cocher:
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/25 (UTC).
[null,null,["Dernière mise à jour le 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"]]