Rich Date Input Widget for Symfony 1.1 Forms
by Matt DaumI noticed that for some reason the 1.1 forms do not allow for the rich date input that many people like to use, so I just created a new widget that allows for rich date input. See below for the widget’s code. It uses the input_date_tag widget in the ‘Form’ helper.
/**
* myWidgetFormRichDate is a rich date widget for 1.1+ forms
*
* @author Matt Daum matt [at] setfive.com
*/
class myWidgetFormRichDate extends sfWidgetFormDate
{
/**
* @param array $options An array of options
* @param array $attributes An array of default HTML attributes
*
* @see sfWidgetForm
*/
protected function configure($options = array(), $attributes = array())
{
parent::configure($options, $attributes);
}
/**
* @param string $name The element name
* @param string $value The value displayed in this widget
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
* @param array $errors An array of errors for the field
*
* @return string An HTML tag string
*
* @see sfWidgetForm
*/
public function render($name, $value = null, $attributes = array(), $errors = array())
{
//Get the date input function from Form helper
use_helper('Form');
//Make the widget rich
$attributes['rich']=true;
return input_date_tag($name,$value, $attributes);
}
}
Tags: date form widget, forms, rich date input widget, symfony, symfony forms

September 24th, 2008 at 11:48 am
Thank you !
November 13th, 2008 at 5:19 am
Awesome. I’ve also been looking for this.
Thanks a lot!
December 4th, 2008 at 1:11 pm
thanks a lot!
December 16th, 2008 at 12:34 am
Thanks, this was helpful.
For anyone copy and pasting this widget code. The line
use_helper(’Form’);
has funky single quotes (’) that need to be changed to straight quotes (‘).
December 16th, 2008 at 12:54 am
[...] order to set the value of rich = true in the input_date_tag method, we need to create a custom rich date widget. I simply followed setfive’s [...]
December 26th, 2008 at 2:23 pm
I am to new symfony and using input_date_tag() in my files results in an error.
How would I get this working in symfony 1.2.0?
December 26th, 2008 at 2:27 pm
vfclists- in Symfony 1.2 you don’t need the above widget. Instead you can just use the sfFormExtraPlugin to get the rich date widget. http://www.symfony-project.org/plugins/sfFormExtraPlugin is the link to the plugin, I suggest you use it rather than the above widget.
December 26th, 2008 at 2:34 pm
I am to new symfony and using input_date_tag() in my files results in an error.
How would I get this working in symfony 1.2.0?
Are there any libraries, helpers and plugins I need to add to the forms code?
June 3rd, 2009 at 6:35 pm
This is great – I can’t upgrade to 1.2 just yet, and its a real life saver.
However, I encountered a couple of problems trying to set some configuration options of the rich data tag. I eventually discovered that input_date_tag() expects a string of options in the form ‘key=val key=val’, as opposed to an array. Here’s my kludgy work around:
addOption( ‘calendar_button_txt’, false );
$this->addOption( ‘calendar_button_img’, false );
$this->addOption( ‘css’, ‘skins/aqua/theme’ );
$this->addOption( ‘calendar_options’, false );
$this->addOption( ‘rich’, true );
}
public function render($name, $value = null, $attributes = array(), $errors = array())
{
//Get the date input function from Form helper
use_helper(‘Form’);
$opts = ”;
foreach( array_merge( $attributes, $this->getOptions() ) as $k => $v ) {
$opts .= “$k=$v “;
}
return input_date_tag($name,$value, $opts );
}
}
September 23rd, 2009 at 10:38 am
Great, but in which file do I have to save/add this code ?
September 23rd, 2009 at 11:06 am
Simone-
You just need to take the code from above and you can put it in the /lib folder of the project (or create a folder called ‘widget’ inside /lib if you want to keep it organized). You can name the file something like myWidgetFormRichDate.class.php and then you just call it as you would with any other widget.
February 17th, 2010 at 5:23 am
Hi,
Nice tip that I’ve used a few times now!
However…
This time I wanted to use it in a partial. To get the calendar to load in one though, I had to specifically include /sf/calendar/calendar.js, /sf/calendar/lang/calendar-en.js, /sf/calendar/calendar-setup.js and /sf/calendar/skins/aqua/theme.css in my view.yml
This works great in the partial, but in this instance when validation fails you end up back on a standard template, resulting in everything loading twice (once because I specified in view.yml and once autoloading as they should) which causes a nasty error in calendar.js…
Just thought someone here might have been aware of the way around this? I tried creating a view.yml at the module level and specifying the javascripts and css for my partial while removing them from the app level view.yml but it doesn’t seem to load them unfortunately…
If anyone reading this comes up with the solution would be interested to hear… I think I’ll have to give up and find another approach though…