【发布时间】:2014-02-03 16:28:52
【问题描述】:
我有一个看起来像这样的表单类型:
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('birthdate', 'date'));
}
我的验证者:
Foo\BarBundle\Model\User\Profile:
properties:
birthdate:
- NotNull:
message : label.null
- Date:
message: user.profile.birthdate.invalid
我的请求与请求正文数据:
PUT foo.dev/user/profile
{
"user_profile" : {
"sex" : 0,
"birthdate" : {
"year" : 1983,
"month" : 6,
"day" : 23
},
"height" : 180,
"weight" : 78,
"pal" : 1.2
}
}
这给了我一个 400:
{
"code":400,
"message":"Validation Failed",
"errors":{
"children":{
"birthdate":{
"errors":[
"This value is not valid."
],
"children":{
"year":[],
"month":[],
"day":[]
}
},
}
}
同样的情况,如果我使用这个:
PUT foo.dev/user/profile
{
"user_profile" : {
"sex" : 0,
"birthdate" : "2014-01-01",
"height" : 180,
"weight" : 78,
"pal" : 1.2
}
}
我做错了什么?请帮忙:'(
更新:
如果我使用widget => single_text,在添加表单字段时,我可以使用birthdate 成功请求此端点:1974-05-06,但我希望能够发送更多而不是仅一种格式:
- 日期时间对象
- 时间戳
- YYYY-MM-DD
- 数组
【问题讨论】:
-
我建议你使用data transformers。但我认为接受多种格式需要大量工作来识别然后解析每种格式。祝你好运!
标签: date symfony validation