Development

#3082 (sfValidatorDate should acknowledge date_format option with array values)

You must first sign up to be able to contribute.

Ticket #3082 (assigned enhancement)

Opened 4 months ago

Last modified 3 months ago

sfValidatorDate should acknowledge date_format option with array values

Reported by: paulo_graca Assigned to: Carl.Vondrick (accepted)
Priority: minor Milestone:
Component: validation Version: 1.1.0 DEV
Keywords: date, validation Cc:
Qualification: Unreviewed

Description

Hi!

In the class sfValidatorDate

class sfValidatorDate extends sfValidator
{
  /**
   * Configures the current validator.
   *
   * Available options:
   *
   *  * date_format:       A regular expression that dates must match
   *  * with_time:         true if the validator must return a time, false otherwise
   *  * date_output:       The format to use when returning a date (default to Y-m-d)
   *  * datetime_output:   The format to use when returning a date with time (default to Y-m-d H:i:s)
   *  * date_format_error: The date format to use when displaying an error for a bad_format error
   *
   * Available error codes:
   *
   *  * bad_format
   *
   * @see sfValidator
   */
  protected function configure($options = array(), $messages = array())
  {
    $this->addMessage('bad_format', '"%value%" does not match the date format (%date_format%).');

    $this->addOption('date_format', null);
    $this->addOption('with_time', false);
    $this->addOption('date_output', 'Y-m-d');
    $this->addOption('datetime_output', 'Y-m-d H:i:s');
    $this->addOption('date_format_error');
  }

  /**
   * @see sfValidator
   */
  protected function doClean($value)
  {
    if (is_array($value))
    {
      $clean = $this->convertDateArrayToTimestamp($value);
    }
    else if ($regex = $this->getOption('date_format'))
    {
      if (!preg_match($regex, $value, $match))
      {
        throw new sfValidatorError($this, 'bad_format', array('value' => $value, 'date_format' => $this->getOption('date_format_error') ? $this->getOption('date_format_error') : $this->getOption('date_format')));
      }

in the code part:

 protected function doClean($value)
  {
    if (is_array($value))
    {
      $clean = $this->convertDateArrayToTimestamp($value);
    }

I suggest to add the validation for "date_format" like it is in the lower line:

    $regex = $this->getOption('date_format');
    if (!preg_match($regex, $clean, $match))

This is useful if you have a different date input form, like YYYY-mm or just: YYYY. In this case you can validate if it's correct.

Change History

04/16/08 00:45:11 changed by Carl.Vondrick

  • owner changed from fabien to Carl.Vondrick.
  • status changed from new to assigned.
  • summary changed from Sugestion for sfValidatorDate to sfValidatorDate should acknowledge date_format option with array values.