intro
This article shows usage of Dojo widgets(dijit) with Zend_Form. Using dijit, it is possible to input date with calendar etc.
You can see the screens from the demonstration site.
You can see the screens from the demonstration site.
1.Create Controller
Create the file
application/constorllers/DojoController.php
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
<?php require_once 'Zend/Controller/Action.php'; require_once 'Zend/Form.php'; require_once 'Zend/Locale.php'; class DojoController extends Zend_Controller_Action { public function getForm() { $form = new Zend_Form(); $form->setName('calendarForm') ->setAction($this->_request->getBaseUrl() . '/dojo/index') ->setMethod('post'); $decorators = array( 'ViewHelper', array('HtmlTag', array('tag' => 'dt', 'class' => 'submit')) ); $options = array(1=>'red', 'green', 'blue'); $element = $form->createElement('select', 'color'); $element->setLabel('Color') ->setAttrib('dojoType', array('dijit.form.FilteringSelect')) ->setRequired(true) ->setMultiOptions($options) ->addValidator('inArray', false, array(array_keys($options))); $form->addElement($element); $element = $form->createElement('multiCheckbox', 'color2'); $element->setLabel('Color2') ->setAttrib('dojoType', array('dijit.form.CheckBox')) ->setRequired(true) ->setMultiOptions($options) ->addValidator('inArray', false, array(array_keys($options))); $form->addElement($element); $element = $form->createElement('text', 'calendar'); $element->setLabel('Date') ->setAttrib('dojoType', array('dijit.form.DateTextBox')) ->setAttrib('constraints', "{datePattern:'yyyy-MM-dd'}") ->setRequired(true) ->addFilter('stringTrim') ->addValidator('date'); $form->addElement($element); $element = $form->createElement('text', 'time'); $element->setLabel('Time') ->setAttrib('dojoType', array('dijit.form.TimeTextBox')) ->setAttrib('constraints', "{timePattern:'HH:mm:ss'}") ->setRequired(true) ->addFilter('stringTrim') ->addValidator('regex', false, array('/^T([0|1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$/')); $form->addElement($element); $element = $form->createElement('text', 'number'); $element->setLabel('Number') ->setAttrib('dojoType', array('dijit.form.NumberTextBox')) ->setAttrib('editOptions', "{pattern:'#.##'}") ->setRequired(true) ->addFilter('stringTrim') ->addValidator('float'); $form->addElement($element); $element = $form->createElement('text', 'currency'); $element->setLabel('Currency') ->setAttrib('dojoType', array('dijit.form.CurrencyTextBox')) ->setAttrib('currency', "USD") ->setRequired(true) ->addFilter('stringTrim') ->addValidator('float'); $form->addElement($element); $element = $form->createElement('text', 'numberSpinner'); $element->setValue('10') ->setLabel('Number') ->setAttrib('dojoType', array('dijit.form.NumberSpinner')) ->setRequired(true) ->addFilter('stringTrim') ->addValidator('digits'); $form->addElement($element); $submit = $form->createElement('submit', 'submit'); $submit->setLabel('Send') ->setDecorators($decorators); $form->addElement($submit); return $form; } public function indexAction() { $form = $this->getForm(); if ($this->getRequest()->isPost()) { if ($form->isValid($_POST)) { $values = $form->getValues(); $this->view->values = $values; } } $this->view->form = $form; } } |
2.Create View
Create the file
And speficy the style class of body tag.
application/views/scripts/dojo/index.phtml
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<h1><?= $this->translate('Dojo Widget Samples '); ?></h1> <?php if($this->values) : ?> <h3><?= $this->translate('You just submitted the following values'); ?>:</h3> <dl> <?php foreach ($this->values as $key => $value) :?> <dt><?=$this->escape($key); ?></dt> <?php if (is_array($value)) : ?> <dd><?= $this->escape(implode(',', $value)); ?></dd> <?php else : ?> <dd><?= $this->escape($value); ?></dd> <?php endif; ?> <?php endforeach; ?> </dl> <?php else : ?> <?= $this->form; ?> <?php endif; ?> <style type="text/css"> @import "<?= $this->baseUrl ?>/js/dijit/themes/tundra/tundra.css"; </style> <script type="text/javascript"> var djConfig = { isDebug:true, parseOnLoad:true }; </script> <script type="text/javascript" src="<?= $this->baseUrl . '/js/dojo/dojo.js'?>"></script> <script type="text/javascript"> dojo.require("dijit.form.FilteringSelect"); dojo.require("dijit.form.CheckBox"); dojo.require("dijit.form.DateTextBox"); dojo.require("dijit.form.TimeTextBox"); dojo.require("dijit.form.NumberTextBox"); dojo.require("dijit.form.CurrencyTextBox"); dojo.require("dijit.form.NumberSpinner"); </script> |
And speficy the style class of body tag.
1 |
<body class="tundra"> |
3.Check
Access the web server and check the dojo widgets.
History
Date | Content |
---|---|
2008/4/30 | Published |
hi, what about showing some examples using YUI (there is no example on the net) as it’s becoming a major javascript framework.
Best regards,
Nicolas
Hello. Where in the code can I define the body class?
Hello.
If you use layout file, you can write the body class in it.
Otherwise you can write directly the body class in dojo/index.phtml.
About using layout file, please see Programmer’s Reference guide(20.Zend_Layout) or Simple Layout using Zend_Layout.
Thank you.
You should render your form before the viewscript then all you need to do is a:
if ($this->dojo()->isEnabled()){
echo $this->dojo();
}
And you skip the part with the dojo.require() etc. I would also stress the importance of giving the form a unique name to allow programmatic initialization (as correctly provided in the tutorial).
Thank you for your good advice, and latest information.