我的控制器中有以下代码。
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('CIInventoryBundle:DiscountLevelItem')->find($discountLevelItemId);
$form = $this->createForm(new DiscountLevelItemCollectionType(), $entity);
$form->bindRequest($request);
$errors = array();
foreach ($form['discountLevelItemProducts'] as $formField) {
$errors[] = $formField->getErrors();
}
var_dump($errors);
die;
if ($form->isValid()) {
//remove items without discount type
foreach ($entity->getDiscountLevelItemProducts() as $item) {
if (!$item->getDiscountType()) {
$entity->getDiscountLevelItemProducts()->removeElement($item);
$em->remove($item);
}
}
$em->persist($entity);
$em->flush();
$responseData = array(
'status' => 'success',
'message' => 'Supplier product discounts successfully saved.'
);
} else {
$responseData = array(
'status' => 'error',
'form' => $this- >renderView('CIInventoryBundle:DiscountLevel:manageProducts.html.twig', array(
'entity' => $entity,
'form' => $form->createView()
))
);
}
return new Response(json_encode($responseData), 200, array('Content-Type'=>'application/json'));
我提交了以下数据:
ci_inventorybundle_discountlevelitemcollectiontype [_token]:17044e40450d5546f4be40e6fbe28f2866dffcfb ci_inventorybundle_discountlevelitemcollectiontype [discountLevelItemProducts] [2] [货运]:0.00 ci_inventorybundle_discountlevelitemcollectiontype [discountLevelItemProducts] [2] [discountValue]:0.00 ci_inventorybundle_discountlevelitemcollectiontype [discountLevelItemProducts] [2] [discountType]:固定 ci_inventorybundle_discountlevelitemcollectiontype [discountLevelItemProducts] [2] [变型]:1567
当我尝试var_dump $ errors我得到以下数据:
array(2) {
[0]=>
array(0) {
}
[1]=>
array(0) {
}
}
我使用的是Symfony 2.0.23。
我的问题是如何以编程方式获取表单集合的表单错误?
谢谢!
答案 0 :(得分:0)
您可以将getErrorsAsString方法作为示例来获取所需的功能。您还必须在表单字段中设置invalid_message
选项以更改This value is invalid
消息。