开发者

Is there a way to set the default values for sfWidgetFormFilterDate widget?

I have an auto generated BaseBlahBlahBlahFilter.class file in my /lib/filter/base/ folder. It contain the following line for 'data' type field :

'date'         => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => true)),

When the form loads it shows me empty values for all month/day/year drop downs. Is there开发者_开发技巧 a way I can set default values (for example today's date) to that drop downs?


There's a bug in sfWidgetFormSchema that leads to ignoring default widget's values while rendering widgets. After applying a patch you'll be able to just tell

'date'         => new sfWidgetFormFilterDate(array(
    'from_date' => new sfWidgetFormDate(array('default' => date())), 
    'to_date' => new sfWidgetFormDate(array('default' => date())), 
    'with_empty' => true)),

e.g. default option will work.


Setting default values must be done from the Form files, not the filter class. Just look your up your 'BlahBlahBlah.class' file in /lib/form and check if it has a configure() method. If not, create it:

class BlahBlahBlahForm extends sfForm
{
  public function configure()
  {

  }
}

You can always override the base classes with 'normal' classes. Just create the a file with the same name, minus the 'Base'-part and add it to your /lib/form (or /lib/filter) directory.

From there you can do something like:

$this->setDefault('from_date', date('Y/m/d'));
$this->setDefault('to_date', date('Y/m/d'));

In the above example this sets it to the current date. Modify it for your own needs.

A good start for reading is the Forms book. You can find it here.


TheGrandWazoo's answer is correct for setting the defaults for sfWidgetFormDate, however, we're talking about sfWidgetFormFilterDate, which is an extension of sfWidgetFormDateRange.

sfWidgetFormDateRange encapsulates two separate date widgets. In this case, you would set the date doing something like the following:

$form->setDefault('date', array('from' => date('Y/m/d'), 'to' => date('Y/m/d')));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜