使用 Symfony 3 Forms 插入 DateType 字段时出现问题
Issue when inserting DateType Field with Symfony 3 Forms
我搜索了很多,但在 Whosebug 中找不到关于我的问题的任何信息...
我有这个结构:(已恢复)
$form = $this->createFormBuilder($vendedor)
// ...
->add('datanascimento', DateType::class, [
'label' =>'D.Nascimento',
'attr' =>['class'=>'input-sm'],
'format'=>'d-M-yyyy',
'years'=>range(1970, 2010)
])
// ...
->getForm();
我的实体中有这个配置:
//...
/**
* @var \DateTime
*
* @ORM\Column(name="dataNascimento", type="date", nullable=true)
*/
private $datanascimento;
//...
/**
* Set datanascimento
*
* @param string $datanascimento
*
* @return CadPfPj
*/
public function setDatanascimento($datanascimento)
{
$this->datanascimento = $datanascimento;
return $this;
}
/**
* Get datanascimento
*
* @return string
*/
public function getDatanascimento()
{
return $this->datanascimento;
}
当我尝试插入一个新的寄存器时,出现了这个错误:
Catchable Fatal Error: Object of class DateTime could not be converted to string
500 Internal Server Error - ContextErrorException
当我调试并转储对象时...我发现了这个:
//...
-datanascimento: DateTime {#530 ▼
+"date": "1970-01-01 00:00:00.000000"
+"timezone_type": 3
+"timezone": "America/Sao_Paulo"
}
//...
我的数据库是 mySQL,字段类型是日期时间...
我如何配置 symfony 停止发送数组并只发送 "date"?
感谢您的帮助!
您需要更改 widget
type of form entry – 默认为 choice
,但您需要 text
或 single_text
。
->add('datanascimento', DateType::class, [
'label' =>'D.Nascimento',
'widget' =>'single_text',
'attr' =>['class'=>'input-sm'],
'format' =>'d-M-yyyy',
'years' =>range(1970, 2010)
])
我搜索了很多,但在 Whosebug 中找不到关于我的问题的任何信息...
我有这个结构:(已恢复)
$form = $this->createFormBuilder($vendedor)
// ...
->add('datanascimento', DateType::class, [
'label' =>'D.Nascimento',
'attr' =>['class'=>'input-sm'],
'format'=>'d-M-yyyy',
'years'=>range(1970, 2010)
])
// ...
->getForm();
我的实体中有这个配置:
//...
/**
* @var \DateTime
*
* @ORM\Column(name="dataNascimento", type="date", nullable=true)
*/
private $datanascimento;
//...
/**
* Set datanascimento
*
* @param string $datanascimento
*
* @return CadPfPj
*/
public function setDatanascimento($datanascimento)
{
$this->datanascimento = $datanascimento;
return $this;
}
/**
* Get datanascimento
*
* @return string
*/
public function getDatanascimento()
{
return $this->datanascimento;
}
当我尝试插入一个新的寄存器时,出现了这个错误:
Catchable Fatal Error: Object of class DateTime could not be converted to string
500 Internal Server Error - ContextErrorException
当我调试并转储对象时...我发现了这个:
//...
-datanascimento: DateTime {#530 ▼
+"date": "1970-01-01 00:00:00.000000"
+"timezone_type": 3
+"timezone": "America/Sao_Paulo"
}
//...
我的数据库是 mySQL,字段类型是日期时间... 我如何配置 symfony 停止发送数组并只发送 "date"?
感谢您的帮助!
您需要更改 widget
type of form entry – 默认为 choice
,但您需要 text
或 single_text
。
->add('datanascimento', DateType::class, [
'label' =>'D.Nascimento',
'widget' =>'single_text',
'attr' =>['class'=>'input-sm'],
'format' =>'d-M-yyyy',
'years' =>range(1970, 2010)
])