有没有办法设置 sfWidgetFormFilterDate 小部件的默认值?
我的 /lib/filter/base/ 文件夹中有一个自动生成的 BaseBlahBlahBlahFilter.class 文件。它包含以下“数据”类型字段行:
'date' => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => true)),
加载表单时,它会显示所有月/日/年下拉菜单的空值。有没有办法为该下拉菜单设置默认值(例如今天的日期)?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sfWidgetFormSchema
中存在一个错误,导致忽略默认小部件的渲染小部件时的值。应用补丁后,您将能够告诉例如
default
选项将起作用。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 telle.g.
default
option will work.设置默认值必须从表单文件完成,而不是过滤器类。只需在 /lib/form 中查找“BlahBlahBlah.class”文件并检查它是否有configure() 方法。如果没有,请创建它:
您始终可以用“普通”类覆盖基类。只需创建一个具有相同名称的文件,减去“Base”部分并将其添加到您的 /lib/form (或 /lib/filter)目录中。
从那里您可以执行以下操作:
在上面的示例中,这将其设置为当前日期。根据您自己的需要修改它。
阅读《Forms》这本书是一个好的开始。您可以在此处找到它。
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:
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:
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 的答案对于设置 sfWidgetFormDate 的默认值是正确的,但是,我们讨论的是 sfWidgetFormFilterDate,它是 sfWidgetFormDateRange 的扩展。
sfWidgetFormDateRange 封装了两个独立的日期小部件。在这种情况下,您可以执行如下操作来设置日期:
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: