1

This is my controller action

/**
 * @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,
    ));
}

Here i am trying to get embedded collection form errors in array format but i am getting as an string like this

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 "

How can i get child form errors in array format like below

$errors = $errors['0' => array('fieldname' => 'error valie','fiedlname' => 'eerror2'),'1' => array('fieldname' => 'error valie','fiedlname' => 'eerror2')];

I tried below code also

$errors = array();
foreach ($form as $formField) {
    foreach ($formField->getErrors(true) as $error) {          
        $errors[] = $error->getMessage();             
    }
}

it returns array

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

here i am adding two prototype forms every form contain seven errors it return 14 array elements but identification of embedded form errors are not possible. i mean how can i identify first and second form errors

please help me any one Thanks in advance...

i got the solution i followed this post 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;
}
somesh
  • 528
  • 2
  • 10
  • 26

0 Answers0