我创建了一个表单,当我将它发送到服务器时,我收到一个内部服务器错误,因为存在不允许的空字段。但我想知道,因为我使用以下代码检查表单,因此通常应该跳过数据库操作。可能是什么原因?
控制器:
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;