English 中文(简体)
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 )));




相关问题
C# Form Problem: new form losing control and randomly hiding

I m encountering strange behavior with forms on a c# 3.5 app. On a button click, my form1 hides itself, creates a new form2, and shows form2. Form1 also contains the event method triggered when ...

TCPlistener.BeginAcceptSocket - async question

Some time ago I have payed to a programmer for doing multithread server. In the meantime I have learned C# a bit and now I think I can see the slowndown problem - I was told by that guy that nothing ...

RoR: before_save on nested object in form?

I have a form with a nested object (customer < order), and it works except that it keeps creating a new customer record. I d like to have it check to see if an existing customer is already ...

Receive POST from External Form

I have a form on another website (using a different backend) that I want to be able to POST to my Rails application (on a different domain). How do I generate a valid authenticity token for the ...

Getting posted values in MVC PartialView

I ve created a PartialView which I render with Html.RenderPartial, passing the name of the view and the strongly-typed data item to bind to (below): <% Html.RenderPartial("...

Recaptcha - Form Customization

Does anyone know if recaptcha can be fully customize without the default frame. I need the recaptcha image to only be a certain width as well as the input field. Has anyone done this before with ...

Better way to retain form data on manual back redirect?

I have a form that, on submit, requires the customer to look over the data, and then confirm the changes before saving the data. However, I m using a really rough way of retaining data if the user ...

热门标签