HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In
GitHubNuGetDev CommunityDoc feedback

## Create a simple editor widget

The following example shows the basic structure of a widget class and creates a simple widget for entering a valid e-mail address.


Use the [Optimizely eslint-plugin-cms tool](🔗) to verify that only public non-deprecated Optimizely Content Management System (CMS) API's are used when creating widgets.


The code snippet inherits from the `dijit/\_TemplatedMixin` class, which lets you create a template for the user interface of the widget in either a local string or an external HTML file. You can attach to elements in the template, giving you a programmatic reference to that node in the widget, and you also can attach events to event handlers in our widget.

When the widget is created, the initial value is not available. The initial value is set with set('value', value) and can be called multiple times when the editor user interface is loading.  To make sure you update the value in the text box when it is set on the widget, declare a `\_setValueAttr` method:

The `\_setValueAttr` method also references a variable named email, which is the textbox DOM node; it is automatically assigned to this variable name by the `dijit/\_Templated` class when it parses the `data-dojo-attach-point` in the template code.

When you need to populate the changes made in the widget, call the `onChange` method and pass a new value to it. You should call `onChange` as often as necessary during editing to provide an accurate preview of changes.

The `\_onChange` method is a private event handler that is triggered when a change is made on the text box. It is configured in the template using the `data-dojo-attach-event` syntax. After it has updated the value, it calls the `onChange` method which in turn causes a page to update.

The following example shows how to update the user interface when a user types in it. In the `postCreate` method, connect to the `onKeyDown` and `onKeyUp` events on the text box element if the `intermediateChanges` property is set to true.

You can also control where you set the focus when the control loads by implementing the focus method.

A complete example on how to create a custom widget (a `StringList`) can be found in our Alloy template site available on [GitHub](🔗).

Make sure to check out a topic on how to [register a custom editor widget on the server side](🔗) for how to use the custom editor.

## Editor widget properties

There are a few important widget properties worth to mention:

  • `intermediateChanges` – Indicates whether the `onChange` method is used for each value change or only on demand.

  • `label` – Title of the property to be edited.

  • `value` – Value of the widget.

  • `required` – Indicates whether this widget requires a value.


You can add properties for a particular widget by using a `PropertyEditorDescriptor`.

## Editor widget methods

  • `onChange` – Callback event that should be raised from within the widget when the value has changed. The wrapper displaying this widget listens to this event and updates the user interface when it occurs.

  • `focus` – Called when the widget is displayed, should give focus to the start element in the widget.

  • `isValid` – Called during validation when an item is saving; true if the current value is valid.

## Validation

To support validation, the widget must implement the isValid method.

The constraints for a property are mixed into the widget when it is constructed. For example, if the value has the required checkbox selected in admin view, then that is passed through as the property required.

To add a custom error message when validating, the widget must implement the `getErrorMessage` method.

## Manage child dialog boxes

If your widget needs to launch a dialog box of its own, extend an additional class with `epi-cms/widget/\_HasChildDialogMixin`, and set a few property values in the correct place to ensure that the blur event, (going from the main dialog box to the new one), does not close the main dialog box.

The class provides one additional property called `isShowingChildDialog`, which is used in the blur event of the main dialog to determine whether it should hide itself. Therefore, if you want to prevent hiding of the main dialog box when the widget launches a child dialog box, set the value to true before launching the child dialog box, then set it back to false after the child dialog box closes.

## Use this custom editor

or create an `EditorDescriptor`:


Without the `UIHint`, all string properties will be affected and will use the custom EmailEditor widget.

## Related topics

  • [Example StringList editor widget from Alloy MVC](🔗)

  • [Optimizely eslint-plugin-cms tool](🔗)