Zend Framework and jQuery Datepicker (updated)

In my last post, I showed how to use jQuery datepicker(“option”, [option | callback]) in my viewscript to add on my datepicker, so the #start_date and #end_date can interactive with each other. Today I found a better way to do that, so that I do not have to include the javascript code in my viewscript. By using Zend_Json_Expr, I can insert my code into jQueryParams. Here is my updated form, and you can see how it works.

class Test_Form extends ZendX_JQuery_Form {
		
		$elem = new ZendX_JQuery_Form_Element_DatePicker('start_date', array(
					'label' 		=> 'Start Date',
					'required'		=> true,
					'jQueryParams'	=> array(
						'dateFormat' => 'dd-mm-yy',
						'minDate'	 => '+1',
						'onSelect'		=> new Zend_Json_Expr('
									function( selectedDate ) 
									{					
										var option = "minDate";
										instance = $( this ).data( "datepicker" );
										date = $.datepicker.parseDate(
											instance.settings.dateFormat,
											selectedDate, instance.settings );
										$("#end_date").datepicker( "option", option, date );
									}')
		)));
		
		$this->addElement($elem);
		
		$elem = new ZendX_JQuery_Form_Element_DatePicker('end_date', array(
					'label' 		=> 'End Date',
					'required'		=> true,
					'jQueryParams'	=> array(
						'dateFormat' 	=> 'dd-mm-yy',
						'minDate'	 	=> '+1',
						'defaultDate' 	=> '+1',
						'onSelect'		=> new Zend_Json_Expr('
									function( selectedDate ) 
									{					
										var option = "maxDate";
										instance = $( this ).data( "datepicker" );
										date = $.datepicker.parseDate(
											instance.settings.dateFormat,
											selectedDate, instance.settings );
										$("#start_date").datepicker( "option", option, date );
									}')
		
					
		)));

		$this->addElement($elem);
}

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

2 thoughts on “Zend Framework and jQuery Datepicker (updated)

  1. Gratz man, Zend_Json_Expr did the trick. I was not getting to make it work until I find your post.

    Nice post.

    Thanks you.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA image
*