I have a Parents form embedded into another form Student containing the data of the parents of a student. I need to validate the embedded form, because in my code just makes the validation of another form.
StudentType.php
//...
->add('responsible1', new ParentsType(),array('label' => 'Mother'))
->add('responsible2', new ParentsType(),array('label'=> 'Father'))
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'BackendBundle\Entity\Student'
));
}
Entity Parents
//...
/**
* @ORM\OneToMany(targetEntity="Student", mappedBy="$responsible1")
* @ORM\OneToMany(targetEntity="Student", mappedBy="$responsible2")
*/
private $students;
Entity Student
//...
/**
*
* @ORM\ManyToOne(targetEntity="Parents", inversedBy="students", cascade={"persist"})
*/
private $responsible1;
/**
*
* @ORM\ManyToOne(targetEntity="Parents", inversedBy="students", cascade={"persist"})
*/
private $responsible2;
Using the following code in the controller I got the name and the error message of all invalid fields in the main form (Student), but I get get errors embedded forms (Parents), just get the name of the object (responsible1 or responsible2) and the message I get [object Object].
StudentController.php
protected function getErrorMessages(\Symfony\Component\Form\Form $form)
{
$errors = array();
foreach ($form->getErrors() as $key => $error) {
$errors[] = $error->getMessage();
}
foreach ($form->all() as $child) {
if (!$child->isValid()) {
$errors[$child->getName()] = $this->getErrorMessages($child);
}
}
return $errors;
}
/**
* Creates a new Student entity.
*
*/
public function createAction(Request $request)
{
// if request is XmlHttpRequest (AJAX) but not a POSt, throw an exception
if ($request->isXmlHttpRequest() && !$request->isMethod('POST')) {
throw new HttpException('XMLHttpRequests/AJAX calls must be POSTed');
}
$entity = new Student();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
if ($request->isXmlHttpRequest()) {
return new JsonResponse(array('message' => 'Success!'), 200);
}
return $this->redirect($this->generateUrl('student_show', array('id' => $entity->getId())));
}
if ($request->isMethod('POST')) {
return new JsonResponse(array(
'result' => 0,
'message' => 'Invalid form',
'data' => $this->getErrorMessages($form)),400);
}
return $this->render('BackendBundle:Student:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
I tried the above code with the function getErrorsAsString() for errors in a string and so if they appear all, so I guess I'll have to add something in the above code to validate objects when "responsible1" or "responsible2" validate all fields.
I need to get all those errors are invalid fields on both forms.I read something to add 'cascade_validation' => true , validation_group or @Assert\Valid() by the code, but I tried and I failed to get it. If someone can explain to me a little worth those, I thank you because I'm new to all this.