Php 如何获取数组格式的Symfony3表单验证错误
这是我的控制器动作Php 如何获取数组格式的Symfony3表单验证错误,php,arrays,forms,symfony,Php,Arrays,Forms,Symfony,这是我的控制器动作 /** * @Route("/listing/bedroom/add", name="listingbedroomaddpage") */ public function bedroomAddAction(Request $request) { $em = $this->getDoctrine()->getManager(); $listing = new Listing(); $form = $this->createForm
/**
* @Route("/listing/bedroom/add", name="listingbedroomaddpage")
*/
public function bedroomAddAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$listing = new Listing();
$form = $this->createForm(ListingBedroomType::class, $listing);
$form->handleRequest($request);
$data = $this->buildErrorArray($form);
var_dump($data);
exit;
if ($form->isSubmitted() && $form->isValid()) {
$bedrooms = $listing->getBedrooms();
foreach ($bedrooms as $bedroom){
$listing->addBedroom($bedroom);
$bedroom->setListing($listing);
$em->persist($bedroom);
}
$em->flush();
return new Response('Bedroom details saved successfully');
}
return $this->render('EpitaHousingBundle:Listing:listingbedroomaddpage.html.twig', array(
'form' => $form->createView(),
'errors' => $data,
));
}
在这里,我试图以数组格式获取嵌入的集合表单错误,但得到的是如下字符串
string(774) "ERROR: Included Bills cannot be blank ERROR: Suitable for cannot be blank ERROR: Max allowed occupants cannot be blank ERROR: Description cannot be blank ERROR: Rentamount cannot be blank ERROR: Bondamount cannot be blank ERROR: Area cannot be blank bedrooms: 0: description: ERROR: Description cannot be blank rentamount: ERROR: Rentamount cannot be blank bondamount: ERROR: Bondamount cannot be blank maxallowedoccupants: ERROR: Max allowed occupants cannot be blank includedbills: ERROR: Included Bills cannot be blank suitabilities: ERROR: Suitable for cannot be blank area: value: ERROR: Area cannot be blank "
如何获得如下数组格式的子窗体错误
$errors = $errors['0' => array('fieldname' => 'error valie','fiedlname' => 'eerror2'),'1' => array('fieldname' => 'error valie','fiedlname' => 'eerror2')];
我也尝试了下面的代码
$errors = array();
foreach ($form as $formField) {
foreach ($formField->getErrors(true) as $error) {
$errors[] = $error->getMessage();
}
}
它返回数组
array(14) { [0]=> string(27) "Description cannot be blank" [1]=> string(26) "Rentamount cannot be blank" [2]=> string(26) "Bondamount cannot be blank" [3]=> string(37) "Max allowed occupants cannot be blank" [4]=> string(30) "Included Bills cannot be blank" [5]=> string(28) "Suitable for cannot be blank" [6]=> string(20) "Area cannot be blank" [7]=> string(27) "Description cannot be blank" [8]=> string(26) "Rentamount cannot be blank" [9]=> string(26) "Bondamount cannot be blank" [10]=> string(37) "Max allowed occupants cannot be blank" [11]=> string(30) "Included Bills cannot be blank" [12]=> string(28) "Suitable for cannot be blank" [13]=> string(20) "Area cannot be blank" }
这里我添加了两个原型表单,每个表单包含7个错误,它返回14个数组元素,但无法识别嵌入的表单错误。我的意思是如何识别第一和第二个表单错误
请帮助我任何一个提前谢谢
我得到了解决方案,我跟随了这篇文章
你能展示你的
Listing.php
实体吗?@Lukasz确实实体很大,它包含两千多行你可能有*@Assert\NotBlank()
在注释中。是的,我做的每件事都是正确的,现在我正在使用FormInterface获取所有表单errors@Lukasz非常感谢您宝贵的支持您能展示您的列表.php
实体吗?@Lukasz确实实体很大,它包含两千多行您可能有*@Assert\NotBlank()
在注释中。是的,我做的每件事都是正确的,现在我正在使用FormInterface获取所有表单errors@Lukasz非常感谢您的宝贵支持
public function buildErrorArray(FormInterface $form){
$errors = [];
foreach ($form->all() as $child) {
$errors = array_merge(
$errors,
$this->buildErrorArray($child)
);
}
foreach ($form->getErrors() as $error) {
$errors[$error->getCause()->getPropertyPath()] = $error->getMessage();
}
return $errors;
}