Symfony2表单中的验证不起作用

Symfony2表单中的验证不起作用,symfony,Symfony,我已经创建了一个表单,当我将它发送到服务器时,我会收到一个内部服务器错误,因为存在不允许的空字段。但是我想知道,因为我用下面的代码检查表单,所以通常应该跳过数据库操作。原因可能是什么 控制器: public function newAction(Request $request) { $objTrip = new Trip(); $objForm = $this->createForm(new TripType, $objTrip); if ($request-

我已经创建了一个表单,当我将它发送到服务器时,我会收到一个内部服务器错误,因为存在不允许的空字段。但是我想知道,因为我用下面的代码检查表单,所以通常应该跳过数据库操作。原因可能是什么

控制器:

public function newAction(Request $request) {

    $objTrip = new Trip();
    $objForm = $this->createForm(new TripType, $objTrip);

    if ($request->isMethod('POST')) {
        $objForm->bind($request);

        if ($objForm->isValid()) {
            $objEm = $this->getDoctrine()->getManager();
            $objEm->persist($objTrip);
            $objEm->flush();

            $response = new Response(json_encode(array('success' => true)));
            $response->headers->set('Content-Type', 'application/json');
            return $response;
        }

        // Es traten Fehler auf
        $arrErrors = array();
        foreach($objForm as $objField) {

            if($objField->hasErrors())
                foreach($objField->getErrors() as $objError)
                    $arrErrors[] = array($objField->var['id'] => $objError->getMessage());
        }
        $response = new Response(json_encode(array('success' => false, 'errors' => $arrErrors)));
        $response->headers->set('Content-Type', 'application/json');
        return $response;
    }

    return array('form' => $objForm->createView());
}
实体:

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=100)
 * @Assert\MinLength(
 *     limit=3
 * )
 */
protected $startLocation;

/**
 * @ORM\Column(type="string", length=100)
 */
protected $endLocation;

/**
 * @ORM\Column(type="datetime")
 */
protected $startTime;

/**
 * @ORM\Column(type="decimal", scale=2)
 */
protected $price;

@Gerrit,你好,看起来你没有使用NotBlank约束。MinLength跳过空值。

当您在formtype中获得实体中不存在的字段时,通常会出现您提到的错误


因此,您能给我们看一下FormType吗?

@Flask,不仅如此——对我来说,当我不为notnull字段设置正确的NotBlank验证器并关闭html5验证或进行功能测试时,通常会出现内部异常),但ofc会很好地查看FormType:)


顺便说一句(很抱歉不直接评论)我只能评论自己的答案是正常的吗?

显示您为实体定义的验证器/***@ORM\Id*@ORM\Column(type=“integer”)*@ORM\GeneratedValue(strategy=“AUTO”)/protected$Id;/**@ORM\Column(type=“string”,length=100)*@Assert\MinLength(*limit=3*)/protected$string;/**@ORM\Column(type=“string”,length=100)/protected$endLocation;/**@ORM\Column(type=“datetime”)/protected$startTime;/**@ORM\Column(type=“decimal”,scale=2)*/protected$price;你能同时提供表格类型吗?
 /**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=100)
 * @Assert\MinLength(
 *     limit=3
 * )
 */
protected $startLocation;

/**
 * @ORM\Column(type="string", length=100)
 */
protected $endLocation;

/**
 * @ORM\Column(type="datetime")
 */
protected $startTime;

/**
 * @ORM\Column(type="decimal", scale=2)
 */
protected $price;