简体   繁体   中英

Symfony FOSREST API Datetime Error

I'm using symfony 2.7, I have created the API, When I submit the form(json data) using postman, It is not accepting datetime value.

Form Builder

->add('letAt','datetime')

Command

    /**
     * @var DateTime
     * @Assert\NotNull(message="Please enter let at time.")
     */
    protected $letAt;

Entity

/**
 * @var \DateTime
 * @ORM\Column(type="datetime", nullable=true)
 */
protected $letAt;

Request Json

{
"property_type":
    {
        "marketingTitle": "",
        "letAt": "2018-11-11 10:10:10"
    }
}

Error

"errors": {
        "children": {
            "marketingTitle": {},
            "letAt": {
                "errors": [
                    "This value is not valid."
                ],
                "children": {
                    "date": {
                        "children": {
                            "year": {},
                            "month": {},
                            "day": {}
                        }
                    },
                    "time": {
                        "children": {
                            "hour": {},
                            "minute": {}
                        }
                    }
                }
            },

How can I specify dateTime value with Request?

In Symfony 2.7 the default datetime format is \\DateTimeType::HTML5_FORMAT and it's value yyyy-MM-dd'T'HH:mm:ssZZZZZ .

You specify your format type like that:

->add('letAt','datetime', array('format' => 'yyyy-MM-dd  HH:mm:ss', 'widget' => 'single_text'))

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