【问题标题】:Validating a date with symfony using sfValidatorSchema使用 sfValidatorSchema 使用 symfony 验证日期
【发布时间】:2010-06-03 07:57:39
【问题描述】:

我正在使用以下小部件来允许用户输入日期

$this->widgetSchema['first_registration'] = new sfWidgetFormDate(array(
  'format' => '%month%/%year%',
  'years' => range(date('Y', time()) - 15, date('Y', time()) + 15)
));

使用此小部件的日期的默认格式是 %month%/%day%/%year%,因此这是此小部件的默认验证器检查的内容。但是,我试图更改验证器...

$this->validatorSchema['first_registration'] = new sfValidatorDate(array(
  'date_format' => '%month%/%year%',
  'with_time' => false
));

这不起作用,我仍然收到无效错误。有人知道为什么吗?

【问题讨论】:

    标签: php symfony1


    【解决方案1】:

    您的小部件和验证器仍包含日期字段。

    您的格式参数所做的只是阻止显示日期 - 它仍然存在并且正在幕后进行验证。我预计这就是问题所在。

    我建议最简单的解决方法是通过覆盖相关表单上的绑定方法将日期默认为 1。不知道你是怎么做的,但是当我处理月/年日期时,我经常在数据库中这样做,因为它允许本地日期/时间格式正确工作——同样的论点适用于 symfony 表单。

    // untested code - i may have array structure/method signatures incorrect.
    function bind($taintedValues = array(), $taintedFiles = array()) {
      $taintedValues['first_registration']['day'] = 1;
      return parent::bind($taintedValues, $taintedFiles);
    }
    

    此外,sfValidatorDate 的 date_format 选项将正则表达式作为其参数,而不是 sfWidgetFormDate 中使用的格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多