我有一个表格:
// AppBundle\Form\MyFormType.php
//...
->add('startDate', 'date', array(
'widget'=>'single_text'
))
//....
一切正常,除非日期是1969/04/27.
**Message:** This value is not valid.
**Origin:** startDate
**Cause:**
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[startDate] = 27/04/1969
**Caused by:**
Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "startDate": Date parsing failed: U_PARSE_ERROR
**Caused by:**
Symfony\Component\Form\Exception\TransformationFailedException
Date parsing failed: U_PARSE_ERROR
我测试了Symfony2的2.6和2.7版本.问题都是一样的.我还测试了不同的应用程序,问题类似.
最佳答案 那是因为你使用php Intl.我使用symfony Intl解决了这个问题.
这是我使用的代码.
在DateTimeToLocalizedStringTransformer.php类中
use Symfony\Component\Intl\DateFormatter\IntlDateFormatter;
class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
{
....
....
....
/**
* Returns a preconfigured IntlDateFormatter instance.
*
* @return IntlDateFormatter
*
* @throws TransformationFailedException in case the date formatter can not be constructed.
*/
protected function getIntlDateFormatter()
{
$dateFormat = $this->dateFormat;
$timeFormat = $this->timeFormat;
$timezone = $this->outputTimezone;
$calendar = $this->calendar;
$pattern = $this->pattern;
//remove
//$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
//add
$intlDateFormatter = new IntlDateFormatter('en', $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
// new \intlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/bug.php?id=66323
if (!$intlDateFormatter) {
throw new TransformationFailedException(intl_get_error_message(), intl_get_error_code());
}
$intlDateFormatter->setLenient(false);
return $intlDateFormatter;
}
}
但这只适用于语言环境“en”,直到为其他语言环境实现