Dojo Form Elements and DecoratorsBuilding on the dijit view helpers, the Zend_Dojo_Form family of classes provides the ability to utilize Dijits natively within your forms. There are three options for utilizing the Dojo form elements with your forms:
Example #1 Enabling Dojo in your existing forms "But wait," you say; "I'm already extending Zend_Form with my own custom form class! How can I Dojo-enable it?'" First, and easiest, simply change from extending Zend_Form to extending Zend_Dojo_Form, and update any places where you instantiate Zend_Form_SubForm to instantiate Zend_Dojo_Form_SubForm. A second approach is to call Zend_Dojo::enableForm() within your custom form's init() method; when the form definition is complete, loop through all SubForms to dojo-enable them:
Usage of the dijit-specific form decorators and elements is just like using any other form decorator or element. Dijit-Specific Form DecoratorsMost form elements can use the DijitElement decorator, which will grab the dijit parameters from the elements, and pass these and other metadata to the view helper specified by the element. For decorating forms, sub forms, and display groups, however, there are a set of decorators corresponding to the various layout dijits. All dijit decorators look for the dijitParams property of the given element being decorated, and push them as the $params array to the dijit view helper being used; these are then separated from any other properties so that no duplication of information occurs. DijitElement DecoratorJust like the ViewHelper decorator, DijitElement expects a helper property in the element which it will then use as the view helper when rendering. Dijit parameters will typically be pulled directly from the element, but may also be passed in as options via the dijitParams key (the value of that key should be an associative array of options). It is important that each element have a unique ID (as fetched from the element's getId() method). If duplicates are detected within the dojo() view helper, the decorator will trigger a notice, but then create a unique ID by appending the return of uniqid() to the identifier. Standard usage is to simply associate this decorator as the first in your decorator chain, with no additional options. Example #2 DijitElement Decorator Usage
DijitForm DecoratorThe DijitForm decorator is very similar to the Form decorator; in fact, it can be used basically interchangeably with it, as it utilizes the same view helper name ('form'). Since dijit.form.Form does not require any dijit parameters for configuration, the main difference is that the dijit form view helper require that a DOM ID is passed to ensure that programmatic dijit creation can work. The decorator ensures this, by passing the form name as the identifier. DijitContainer-based DecoratorsThe DijitContainer decorator is actually an abstract class from which a variety of other decorators derive. It offers the same functionality of DijitElement, with the addition of title support. Many layout dijits require or can utilize a title; DijitContainer will utilize the element's legend property, if available, and can also utilize either the 'legend' or 'title' decorator option, if passed. The title will be translated if a translation adapter with a corresponding translation is present. The following is a list of decorators that inherit from DijitContainer:
Example #3 DijitContainer Decorator Usage
Dijit-Specific Form ElementsEach form dijit for which a view helper is provided has a corresponding Zend_Form element. All of them have the following methods available for manipulating dijit parameters:
Dijit parameters are stored in the dijitParams public property. Thus, you can dijit-enable an existing form element simply by setting this property on the element; you simply will not have the above accessors to facilitate manipulating the parameters. Additionally, dijit-specific elements implement a different list of decorators, corresponding to the following:
In effect, the DijitElement decorator is used in place of the standard ViewHelper decorator. Finally, the base Dijit element ensures that the Dojo view helper path is set on the view. A variant on DijitElement, DijitMulti, provides the functionality of the Multi abstract form element, allowing the developer to specify 'multiOptions' -- typically select options or radio options. The following dijit elements are shipped in the standard Zend Framework distribution. ButtonWhile not deriving from the standard Button element, it does implement the same functionality, and can be used as a drop-in replacement for it. The following functionality is exposed:
Additionally, only the decorators DijitElement and DtDdWrapper are utilized for Button elements. Example #4 Example Button dijit element usage
CheckBoxWhile not deriving from the standard Checkbox element, it does implement the same functionality. This means that the following methods are exposed:
Example #5 Example CheckBox dijit element usage
ComboBox and FilteringSelectAs noted in the ComboBox dijit view helper documentation, ComboBoxes are a hybrid between select and text input, allowing for autocompletion and the ability to specify an alternate to the options provided. FilteringSelects are the same, but do not allow arbitrary input.
The ComboBox and FilteringSelect form elements provide accessors and mutators for examining and setting the select options as well as specifying a dojo.data datastore (if used). They extend from DijitMulti, which allows you to specify select options via the setMultiOptions() and setMultiOption() methods. In addition, the following methods are available:
By default, if no dojo.data store is registered with the element, this element registers an InArray validator which validates against the array keys of registered options. You can disable this behavior by either calling setRegisterInArrayValidator(false), or by passing a FALSE value to the registerInArrayValidator configuration key. Example #6 ComboBox dijit element usage as select input
Example #7 ComboBox dijit element usage with datastore
The above examples could also utilize FilteringSelect instead of ComboBox. CurrencyTextBoxThe CurrencyTextBox is primarily for supporting currency input. The currency may be localized, and can support both fractional and non-fractional values. Internally, CurrencyTextBox derives from NumberTextBox, ValidationTextBox, and TextBox; all methods available to those classes are available. In addition, the following constraint methods can be used:
Example #8 Example CurrencyTextBox dijit element usage
DateTextBoxDateTextBox provides a calendar drop-down for selecting a date, as well as client-side date validation and formatting. Internally, DateTextBox derives from ValidationTextBox and TextBox; all methods available to those classes are available. In addition, the following methods can be used to set individual constraints:
Example #9 Example DateTextBox dijit element usage
EditorEditor provides a WYSIWYG editor that can be used to both create and edit rich HTML content. dijit.Editor is pluggable and may be extended with custom plugins if desired; see » the dijit.Editor documentation for more details. The Editor form element provides a number of accessors and mutators for manipulating various dijit parameters, as follows:
Example #10 Example Editor dijit element usage
HorizontalSliderHorizontalSlider provides a slider UI widget for selecting a numeric value in a range. Internally, it sets the value of a hidden element which is submitted by the form. HorizontalSlider derives from the abstract Slider dijit element. Additionally, it has a variety of methods for setting and configuring slider rules and rule labels.
Example #11 Example HorizontalSlider dijit element usage The following will create a horizontal slider selection with integer values ranging from -10 to 10. The top will have labels at the 20%, 40%, 60%, and 80% marks. The bottom will have rules at 0, 50%, and 100%. Each time the value is changed, the hidden element storing the value will be updated.
NumberSpinnerA number spinner is a text element for entering numeric values; it also includes elements for incrementing and decrementing the value by a set amount. The following methods are available:
Example #12 Example NumberSpinner dijit element usage
NumberTextBoxA number text box is a text element for entering numeric values; unlike NumberSpinner, numbers are entered manually. Validations and constraints can be provided to ensure the number stays in a particular range or format. Internally, NumberTextBox derives from ValidationTextBox and TextBox; all methods available to those classes are available. In addition, the following methods can be used to set individual constraints:
Example #13 Example NumberTextBox dijit element usage
PasswordTextBoxPasswordTextBox is simply a ValidationTextBox that is tied to a password input; its sole purpose is to allow for a dijit-themed text entry for passwords that also provides client-side validation. Internally, PasswordTextBox derives from ValidationTextBox and TextBox; all methods available to those classes are available. Example #14 Example PasswordTextBox dijit element usage
RadioButtonRadioButton wraps standard radio input elements to provide a consistent look and feel with other dojo dijits. RadioButton extends from DijitMulti, which allows you to specify select options via the setMultiOptions() and setMultiOption() methods. By default, this element registers an InArray validator which validates against the array keys of registered options. You can disable this behavior by either calling setRegisterInArrayValidator(false), or by passing a FALSE value to the registerInArrayValidator configuration key. Example #15 Example RadioButton dijit element usage
SimpleTextareaSimpleTextarea acts primarily like a standard HTML textarea. However, it does not support either the rows or cols settings. Instead, the textarea width should be specified using standard CSS measurements. Unlike Textarea, it will not grow automatically Example #16 Example SimpleTextarea dijit element usage
Slider abstract elementSlider is an abstract element from which HorizontalSlider and VerticalSlider both derive. It exposes a number of common methods for configuring your sliders, including:
Example usage is provided with each concrete extending class. SubmitButtonWhile there is no Dijit named SubmitButton, we include one here to provide a button dijit capable of submitting a form without requiring any additional javascript bindings. It works exactly like the Button dijit. Example #17 Example SubmitButton dijit element usage
TextBoxTextBox is included primarily to provide a text input with consistent look-and-feel to the other dijits. However, it also includes some minor filtering and validation capabilities, represented in the following methods:
Example #18 Example TextBox dijit element usage
TextareaTextarea acts primarily like a standard HTML textarea. However, it does not support either the rows or cols settings. Instead, the textarea width should be specified using standard CSS measurements; rows should be omitted entirely. The textarea will then grow vertically as text is added to it. Example #19 Example Textarea dijit element usage
TimeTextBoxTimeTextBox is a text input that provides a drop-down for selecting a time. The drop-down may be configured to show a certain window of time, with specified increments. Internally, TimeTextBox derives from DateTextBox, ValidationTextBox and TextBox; all methods available to those classes are available. In addition, the following methods can be used to set individual constraints:
Example #20 Example TimeTextBox dijit element usage The following will create a TimeTextBox that displays 2 hours at a time, with increments of 10 minutes.
ValidationTextBoxValidationTextBox provides the ability to add validations and constraints to a text input. Internally, it derives from TextBox, and adds the following accessors and mutators for manipulating dijit parameters:
Example #21 Example ValidationTextBox dijit element usage The following will create a ValidationTextBox that requires a single string consisting solely of word characters (i.e., no spaces, most punctuation is invalid).
VerticalSliderVerticalSlider is the sibling of HorizontalSlider, and operates in every way like that element. The only real difference is that the 'top*' and 'bottom*' methods are replaced by 'left*' and 'right*', and instead of using HorizontalRule and HorizontalRuleLabels, VerticalRule and VerticalRuleLabels should be used. Example #22 Example VerticalSlider dijit element usage The following will create a vertical slider selection with integer values ranging from -10 to 10. The left will have labels at the 20%, 40%, 60%, and 80% marks. The right will have rules at 0, 50%, and 100%. Each time the value is changed, the hidden element storing the value will be updated.
Dojo Form ExamplesExample #23 Using Zend_Dojo_Form The easiest way to utilize Dojo with Zend_Form is to utilize Zend_Dojo_Form, either through direct usage or by extending it. This example shows extending Zend_Dojo_Form, and shows usage of all dijit elements. It creates four sub forms, and decorates the form to utilize a TabContainer, showing each sub form in its own tab.
Example #24 Modifying an existing form to utilize Dojo Existing forms can be modified to utilize Dojo as well, by use of the Zend_Dojo::enableForm() static method. This first example shows decorating an existing form instance:
Alternately, you can make a slight tweak to your form initialization:
Of course, if you can do that... you could and should simply alter the class to inherit from Zend_Dojo_Form, which is a drop-in replacement of Zend_Form that's already Dojo-enabled...
|