I'm creating a Zend Form in which I want to insert a date span. The form is enhanced using the jQuery UI library, so I have a date from and a date to field, which are both datepicker elements. This is working fine; here is this part of the Zend_Form:
// Date range
$date = $this->getView()->date(time());
$this->addElement('DatePicker', 'userday_from', array(
'label' => 'Datum van',
'value' => $date,
'jQueryParams' => array(
'defaultDate' => $date,
'dateFormat' => $this->getView()->date()->getFormatJquery()
)
));
$this->addElement('DatePicker', 'userday_to', array(
'label' => 'Datum tot',
'value' => $date,
'jQueryParams' => array(
'defaultDate' => $date,
'dateFormat' => $this->getView()->date()->getFormatJquery()
)
));
When the first date is changed, I want to update the second date. jQuery datepicker has an onSelect event for this. However, I'm unable to add a function callback to the jQueryParams:
$this->addElement('DatePicker', 'userday_from', array(
'label' => 'Datum van',
'value' => $date,
'jQueryParams' => array(
'defaultDate' => $date,
'dateFormat' => $this->getView()->date()->getFormatJquery(),
'onSelect' => "function(){ alert('test'); }" // ! is not working
)
));
I therefore try to add this jQuery 'option' afterwards like this:
$this->getView()->jQuery()
->addOnload("
$('#userday_from').datepicker('option', 'onSelect', function(selectedDate){
alert('test');
});
");
This, on the other hand, adds the new jQuery in the document.ready() function, BEFORE the intialization functions of my datepicker elements and thus doesn't work either:
<script type="text/javascript">//<![CDATA[
$(document).ready(function() {
$('#userday_from').datepicker('option', 'onSelect', function(selectedDate){
alert('test');
});
$("#userday_from").datepicker({"defaultDate":"09-11-2012","dateFormat":"dd-mm-yy"});
$("#userday_to").datepicker({"defaultDate":"09-11-2012","dateFormat":"dd-mm-yy"});
});
//]]></script>
I don't want to overwrite the datePicker viewhelper in order to hack something like this in it. This is something to tie 2 form elements together, so adding it to the Zend_Form init() function seems valid to me.