简体   繁体   中英

Symfony2 date validation fails

I've a form type which looks like this:

/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('birthdate', 'date'));
}

My validator:

Foo\BarBundle\Model\User\Profile:
    properties:
        birthdate:
            - NotNull:
                message : label.null
            - Date:
                message: user.profile.birthdate.invalid

My request with request body data:

PUT foo.dev/user/profile

{ 
    "user_profile" : { 
        "sex" : 0,
        "birthdate" : {
            "year"  : 1983,
            "month" : 6,
            "day"   : 23
        },
        "height" : 180,
        "weight" : 78,
        "pal"    : 1.2
    }
}

This gives me a 400:

{
    "code":400,
    "message":"Validation Failed",
    "errors":{
    "children":{
        "birthdate":{
            "errors":[
                "This value is not valid."
            ],
            "children":{
                "year":[],
                "month":[],
                "day":[]
            }
        },
    }
}

Same happens, If I use this:

PUT foo.dev/user/profile

{ 
    "user_profile" : { 
        "sex" : 0,
        "birthdate" : "2014-01-01",
        "height" : 180,
        "weight" : 78,
        "pal"    : 1.2
    }
}

What I'm doing wrong? Pls help :'(

Update:

If I use widget => single_text , when adding the form field, I can successfully request this endpoint with birthdate : 1974-05-06 , but I want to be able, to send more then only 1 format:

  • Datetime Object
  • Timestamp
  • YYYY-MM-DD
  • Array

Update 2 If I use widget => text, I can send the three sub form fields as expected. But I cant send those values as a string.

Nevertheless....I'll close this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM