ZendX_JQuery View Helpers

ZendX_JQuery Form Elements and Decorators

All View Helpers are pressed into Zend_Form elements or decorators also. They can even be easily integrated into your already existing forms. To enable a Form for Zend_JQuery support you can use two ways: Init your form as $form = new ZendX_JQuery_Form(); or use the static method ZendX_JQuery::enableForm($form) to enable jQuery element support.

General Elements and Decorator Usage

Both elements and decorators of the Zend jQuery Form set can be initialized with the option key jQueryParams to set certain jQuery object related parameters. This jQueryParams array of options matches to the $params variable of the corresponding view helpers. For example:

  1. $element = new ZendX_JQuery_Form_Element_DatePicker(
  2.                     'dp1',
  3.                     array('jQueryParams' => array('defaultDate' => '2007/10/10'))
  4.                 );
  5. // would internally call to:
  6. $view->datePicker("dp1", "", array('defaultDate' => '2007/10/10'), array());

Additionally elements jQuery options can be customized by the following methods:

  • setJQueryParam($name, $value): Set the jQuery option $name to the given value.

  • setJQueryParams($params): Set key value pairs of jQuery options and merge them with the already set options.

  • getJQueryParam($name): Return the jQuery option with the given name.

  • getJQueryParams(): Return an array of all currently set jQuery options.

Each jQuery related Decorator also owns a getJQueryParams() method, to set options you have to use the setDecorators(), addDecorator() or addDecorators() functionality of a form element and set the jQueryParams key as option:

  1. $form->setDecorators(array(
  2.     'FormElements',
  3.     array('AccordionContainer', array(
  4.         'id'          => 'tabContainer',
  5.         'style'       => 'width: 600px;',
  6.         'jQueryParams' => array(
  7.             'alwaysOpen' => false,
  8.             'animated'   => "easeslide"
  9.         ),
  10.     )),
  11.     'Form'
  12. ));

Form Elements

The Zend Framework jQuery Extras Extension comes with the following Form Elements:

  • ZendX_JQuery_Form_Element_AutoComplete: Proxy to AutoComplete View Helper

  • ZendX_JQuery_Form_Element_ColorPicker: Proxy to ColorPicker View Helper

  • ZendX_JQuery_Form_Element_DatePicker: Proxy to DatePicker View Helper

  • ZendX_JQuery_Form_Element_Slider: Proxy to Slider View Helper

  • ZendX_JQuery_Form_Element_Spinner: Proxy to Spinner View Helper

Note: jQuery Decorators: Beware the Marker Interface for UiWidgetElements
By default all the jQuery Form elements use the ZendX_JQuery_Form_Decorator_UiWidgetElement decorator for rendering the jQuery element with its specific view helper. This decorator is inheritly different from the ViewHelper decorator that is used for most of the default form elements in Zend_Form. To ensure that rendering works correctly for jQuery form elements at least one decorator has to implement the ZendX_JQuery_Form_Decorator_UiWidgetElementMarker interface, which the default decorator does. If no marker interface is found an exception is thrown. Use the marker interface if you want to implement your own decorator for the jQuery form element specific rendering.

Form Decorators

The following Decorators come with the Zend Framework jQuery Extension:

  • ZendX_JQuery_Form_Decorator_AccordionContainer: Proxy to AccordionContainer View Helper

  • ZendX_JQuery_Form_Decorator_AccordionPane: Proxy to AccordionPane View Helper

  • ZendX_JQuery_Form_Decorator_DialogContainer: Proxy to DialogContainer View Helper

  • ZendX_JQuery_Form_Decorator_TabContainer: Proxy to TabContainer View Helper

  • ZendX_JQuery_Form_Decorator_TabPane: Proxy to TabPane View Helper

  • ZendX_JQuery_Form_Decorator_UiWidgetElement: Decorator to Display jQuery Form Elements

Utilizing the Container elements is a bit more complicated, the following example builds a Form with 2 SubForms in a TabContainer:

Example #1 SubForms with TabContainer Decorator

The following example makes use of all Form elements and wraps them into 2 subforms that are decorated with a tab container. First we build the basic ZendX_JQuery_Form:

  1. $form = new ZendX_JQuery_Form();
  2. $form->setAction('formdemo.php');
  3. $form->setAttrib('id', 'mainForm');
  4. $form->setAttrib('class', 'flora');
  5.  
  6. $form->setDecorators(array(
  7.     'FormElements',
  8.     array('TabContainer', array(
  9.         'id'          => 'tabContainer',
  10.         'style'       => 'width: 600px;',
  11.     )),
  12.     'Form',
  13. ));

Setting the Form Id (in this case to 'mainForm') is an important step for the TabContainer. It is needed that the subforms can relate to the Container Id in a later form building stage. We now initialize two SubForms that will be decorated with the TabPane decorators:

  1. $subForm1 = new ZendX_JQuery_Form();
  2. $subForm1->setDecorators(array(
  3.     'FormElements',
  4.     array('HtmlTag',
  5.           array('tag' => 'dl')),
  6.     array('TabPane',
  7.           array('jQueryParams' => array('containerId' => 'mainForm',
  8.                                         'title' => 'DatePicker and Slider')))
  9. ));
  10.  
  11. $subForm2 = new ZendX_JQuery_Form();
  12. $subForm2->setDecorators(array(
  13.    'FormElements',
  14.    array('HtmlTag',
  15.          array('tag' => 'dl')),
  16.    array('TabPane',
  17.          array('jQueryParams' => array('containerId' => 'mainForm',
  18.                                        'title' => 'AutoComplete and Spinner')))
  19. ));

In this stage it is important that the 'containerId' option is set in each SubForm TabPane, or the subforms cannot relate back to the tab Container and would not be displayed. To enforce this setting, an exception of the type ZendX_JQuery_Exception is thrown if the option is not set. We can now add all the desired elements to the subforms:

  1. // Add Element Date Picker
  2. $elem = new ZendX_JQuery_Form_Element_DatePicker(
  3.                 "datePicker1", array("label" => "Date Picker:")
  4.             );
  5. $elem->setJQueryParam('dateFormat', 'dd.mm.yy');
  6. $subForm1->addElement($elem);
  7.  
  8. // Add Element Spinner
  9. $elem = new ZendX_JQuery_Form_Element_Spinner(
  10.                 "spinner1", array('label' => 'Spinner:')
  11.             );
  12. $elem->setJQueryParams(array('min' => 0, 'max' => 1000, 'start' => 100));
  13. $subForm1->addElement($elem);
  14.  
  15. // Add Slider Element
  16. $elem = new ZendX_JQuery_Form_Element_Slider(
  17.                 "slider1", array('label' => 'Slider:')
  18.             );
  19. $elem->setJQueryParams(array('defaultValue' => '75'));
  20. $subForm2->addElement($elem);
  21.  
  22. // Add Autocomplete Element
  23. $elem = new ZendX_JQuery_Form_Element_AutoComplete(
  24.                 "ac1", array('label' => 'Autocomplete:')
  25.             );
  26. $elem->setJQueryParams(array('source' => array('New York',
  27.                                              'Berlin',
  28.                                              'Bern',
  29.                                              'Boston')));
  30. $subForm2->addElement($elem);
  31.  
  32. // Submit Button
  33. $elem = new Zend_Form_Element_Submit("btn1", array('value' => 'Submit'));
  34. $subForm1->addElement($elem);

Three additional lines are missing to put it all together and we have a jQuery animated form:

  1. $form->addSubForm($subForm1, 'subform1');
  2. $form->addSubForm($subForm2, 'subform2');
  3.  
  4. $formString = $form->render($view);

Example #2 Wrapping a Form into the Dialog Container

The only use for the Dialog Container in Zend Form context is to wrap itself around a form and display it in a dialog. Its important to remember that the order of the decorators has to be different than in the Accordion and Tab Container examples.

  1. // Create new jQuery Form
  2. $form = new ZendX_JQuery_Form();
  3. $form->setAction('formdemo.php');
  4.  
  5. // Wrap the complete form inside a Dialog box
  6. $form->setDecorators(array(
  7.     'FormElements',
  8.     'Form',
  9.     array('DialogContainer', array(
  10.         'id'          => 'tabContainer',
  11.         'style'       => 'width: 600px;',
  12.         'jQueryParams' => array(
  13.             'tabPosition' => 'top'
  14.         ),
  15.     )),
  16. ));
  17.  
  18. // Add Element Spinner
  19. $elem = new ZendX_JQuery_Form_Element_Spinner("spinner1", array('label' => 'Spinner:', 'attribs' => array('class' => 'flora')));
  20. $elem->setJQueryParams(array('min' => 0, 'max' => 1000, 'start' => 100));
  21.  
  22. $form->addElement($elem);

ZendX_JQuery View Helpers