Development

#4163 (range() should not be used to specify years range in sfWidgetFormDate)

You must first sign up to be able to contribute.

Ticket #4163 (closed documentation: fixed)

Opened 1 year ago

Last modified 4 months ago

range() should not be used to specify years range in sfWidgetFormDate

Reported by: faithless Assigned to: faithless
Priority: minor Milestone:
Component: widget Version: 1.2.4
Keywords: sfWidgetFormDate default year Cc:
Qualification: Unreviewed

Description

ex. selected value of the "years" part of rendered date will be wrong because years array index is 0-based if you use range() to generate the years range. Since docs don't mention this trap I think you guys might want to document it or fix it in the code.

symfony-1.1.1 (pear)

Attachments

sfWidgetDate.patch (0.7 kB) - added by camason on 02/22/09 23:56:39.
[PATCH] Improve sfWidgetFormDate constructor documentation

Change History

08/08/08 21:44:45 changed by faithless

  • summary changed from sfWidgetFormDate should not use plain range() generated array to specify years range to range() should not be used to specify years range in sfWidgetFormDate.

08/26/08 10:53:12 changed by nicolas

  • owner changed from fabien to faithless.

Can you be a bit more explicit, I don't understand clearly the problem you describe. Can you provide an example? Thanks.

09/01/08 11:20:43 changed by nicolas

  • status changed from new to closed.
  • resolution set to invalid.

Okay, with the lack of explanations, I flag this one as invalid. Feel free to reopen it if you can provide more details.

10/05/08 05:49:34 changed by bigm

Just to expand on this (for the benefit of anyone finding it while googling more than anything) as I ran across the issue myself today, if you have a sfWidgetFormDate field, and you want to specify the 'years' which can be selected, the immediate thought that springs to mind is to use the range() function: new sfWidgetFormDate(array('years' => range(1920,2000));

However the array range() builds has keys incrementing from 0, so is useless, and causes an 'invalid' verification error.

One way around it is to create a variable of your own with keys and values for the years, then pass it as the 'year' option, like the below.

$years = range(1920, 2000); //Creates array of years between 1920-2000 $years_list = array_combine($years, $years); //Creates new array where key and value are both values from $years list

new sfWidgetFormDate(array('years' => $years_list));

Perhaps symfony could offer a way of automating this with 'year_min' and 'year_max' options for sfWidgetDormDate ?

02/22/09 23:53:26 changed by camason

  • status changed from closed to reopened.
  • version changed from 1.1.0 to 1.2.4.
  • resolution deleted.
  • milestone changed from 1.1.2 to 1.2.5.

Just came across this issue myself today.

The constructor provides the PHPdoc message:

* * years: An array of years for the year select tag (optional)

This led me to believe this would be a straight array, in the format: array(1998, 1999, 2000, 2001)

In-fact, the array needs to be key => value: array(1998 => '1998', 1999 => '1999', 2000 => '2000', 2001 => '2001', 2002 => '2002')

Using array_combine() also works.

Changing the documentation to show something along the lines of "... as $key => $value" would help avoid this confusion.

02/22/09 23:56:39 changed by camason

  • attachment sfWidgetDate.patch added.

[PATCH] Improve sfWidgetFormDate constructor documentation

03/12/09 12:41:04 changed by fabien

The forms book appendix A describes how to do it quite clearly. I will also add a note in the PHPDoc API.

03/12/09 12:42:23 changed by fabien

  • status changed from reopened to closed.
  • resolution set to fixed.

in r16259

03/13/09 21:55:54 changed by FabianLange

  • milestone deleted.

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.