【问题标题】:Symfony2 date validation failsSymfony2 日期验证失败
【发布时间】: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


【解决方案1】:

更新 2 如果我使用小部件 => 文本,我可以按预期发送三个子表单字段。但我不能将这些值作为字符串发送。

不过....我会关闭它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多