小编Fab*_*ice的帖子

Symfony 3 约束验证日期或日期时间

我尝试通过 Symfony (3.2) 中的表单验证来验证日期(或日期时间)。

我正在使用 FOSRestBundle 来使用请求中的 json (因为我尝试开发我的个人 API)

但我尝试了很多格式:

  • 2017-04-09
  • 2009年4月17日
  • 对于日期时间:
    • 2017-04-09 21:12:12
    • 2017-04-09T21:12:12
    • 2017-04-09T21:12:12+01:00
  • ...

但表单无效,我总是收到此错误:此值无效

我的控制器的功能

public function postPlacesAction(Request $request) {
    $place = new Place();
    $form = $this->createForm(PlaceType::class, $place);

    $form->handleRequest($request);

    if ($form->isValid()) {
        return $this->handleView($this->view(null, Response::HTTP_CREATED));
    } else {
        return $this->handleView($this->view($form->getErrors(), Response::HTTP_BAD_REQUEST));
    }
}
Run Code Online (Sandbox Code Playgroud)

我的实体

class Place
{
    /**
     * @var string
     *
     * @Assert\NotBlank(message = "The name should not be blank.")
     */
    protected $name;

    /**
     * @var string
     *
     * @Assert\NotBlank(message = "The address …
Run Code Online (Sandbox Code Playgroud)

validation date constraints symfony fosrestbundle

5
推荐指数
1
解决办法
6536
查看次数

标签 统计

constraints ×1

date ×1

fosrestbundle ×1

symfony ×1

validation ×1