如何以数组格式获取Symfony3表单验证错误

时间:2017-10-23 06:35:17

标签: 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(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" } 

这里我添加两个原型表单,每个表单包含七个错误,它返回14个数组元素但是不可能识别嵌入式表单错误。我的意思是如何识别第一和第二形式错误

请帮助我任何人提前感谢...

我得到了解决方案,我按照这篇文章 symfony2 how to get form validation errors after binding the request to the form

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;
}

0 个答案:

没有答案