Stay organized with collections
Save and categorize content based on your preferences.
blockly > Field > setValidator
Field.setValidator() method
Sets a new validation function for editable fields, or clears a previously set validator.
The validator function takes in the new field value, and returns validated value. The validated value could be the input value, a modified version of the input value, or null to abort the change.
If the function does not return anything (or returns undefined) the new value is accepted as valid. This is to allow for fields using the validated function as a field-level change event notification.
Signature:
setValidator(handler: FieldValidator<T>): void;
Parameters
Parameter |
Type |
Description |
handler |
FieldValidator<T> |
The validator function or null to clear a previous validator. |
Returns:
void
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-18 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-09-18 UTC."],[[["The `setValidator()` method allows assigning a function to validate user input in editable fields, or removing a previously set validation function."],["Validators receive the new field value and return a validated version, potentially modified or null to reject the change."],["If the validator doesn't return a specific value, the input is accepted, offering a mechanism for change notifications."],["This method is used within Blockly's Field class to ensure data integrity and control user interactions."]]],["The `setValidator()` method configures or removes a validation function for editable fields. This function, provided as a `handler`, receives the new field value and returns either a validated value, a modified value, or `null` to prevent the change. Returning nothing or `undefined` accepts the new value. The `handler` parameter can also be set to `null` to clear an existing validator. It takes a `FieldValidator` type and returns `void`.\n"]]