【问题标题】:DateTime validation error only for a specific date仅针对特定日期的 DateTime 验证错误
【发布时间】:2016-01-04 16:17:51
【问题描述】:

我有一个带有生日字段的简单用户个人资料表单

实体:

/**
 * @var string
 *
 * @ORM\Column(name="bdate", type="date")
 */
private $bdate;


表单类型:

$builder->add('bdate', 'birthday', array(
    'input'  => 'datetime',
    'widget' => 'single_text'
));


控制器:

if ($request->getMethod() == 'POST') {
    $form->handleRequest($request);

    if ($form->isValid()) {
        ...
    } else {
        // always here when 28 May 1972 
    }
}


它总是有效但是当日期是 1972-05-28 我有验证错误 此值无效。

在 1972 年 5 月 28 日

使用 MySql 它可以工作,但我的数据库是 Oracle。 我正在使用监听器 OracleSessionInit,对此我没有其他问题。

你能帮帮我吗?我疯了

【问题讨论】:

  • 您可能还应该用您的代码使用的任何语言标记您的问题,因为它不是 Oracle。
  • 对不起,这是我在 StackOverflow 上的第一个问题,我删除了 Oracle 标记并添加了 php

标签: php validation symfony datetime


【解决方案1】:

对于夏令时更改恰好在午夜发生的每个日期都确认了该问题,欧洲/罗马从 1966 年到 1979 年就是这种情况:

https://www.timeanddate.com/time/change/italy/rome

Fedora Linux 15 中的这个 bug 可能与此有关:

https://bugzilla.redhat.com/show_bug.cgi?id=1057104

这似乎也发生在当前的 ubuntu 和 windows 上。

重现问题的 PHP 代码:

<?php
$argh = "1972-05-28";
$timezone = "Europe/Rome";
$fmt = new IntlDateFormatter(
    'it',
    IntlDateFormatter::MEDIUM,
    IntlDateFormatter::NONE,
    $timezone,
    IntlDateFormatter::GREGORIAN,
    'yyyy-MM-dd'
);
echo 'lenient of the formatter is : ';
if ($fmt->isLenient()) {
    echo 'TRUE';
} else {
    echo 'FALSE';
}
$fmt->parse($argh);
echo "\n Trying to do parse($argh).\nResult is : " . $fmt->parse($argh);
if (intl_get_error_code() != 0) {
    echo "\nError_msg is : " . intl_get_error_message();
    echo "\nError_code is : " . intl_get_error_code();
}

$fmt->setLenient(FALSE);
echo "\nNow lenient of the formatter is : ";
if ($fmt->isLenient()) {
    echo 'TRUE';
} else {
    echo 'FALSE';
}
$fmt->parse($argh);
echo "\n Trying to do parse($argh).\nResult is : " . $fmt->parse($argh);
if (intl_get_error_code() != 0) {
    echo "\nError_msg is : " . intl_get_error_message();
    echo "\nError_code is : " . intl_get_error_code();
}

?>

一种可能的解决方法是将时区设置为不同的时区,该时区在午夜时不会完全改变,即“欧洲/巴黎”。

【讨论】:

    【解决方案2】:

    不仅代码不是Oracle,错误消息也不是。但是,由于您为“日期”引用了“好”值和“坏”值,我会假设无效值是针对日期的。您可能将字符串数据与实际的 DATE 数据类型混淆了。您显示为“日期”的内容实际上只不过是您识别为日期的字符串,但是当它归结为由数据库处理的 sql 语句时,必须有一个关于如何解释该字符串的规则。在 oracle 中,这是通过设置 NLS_DATE_FORMATE 或(在我看来更好)使用 TO_DATE 函数来完成的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多