Using the guide http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html i’m trying to customize error message, but i’ve a problem: variable errors is not defined, as we don’t validate an entity and we are not calling $this->get('validator')->validate($entity).
{% block field_errors %}
{% spaceless %}
{# errors is undefined here #}
{% endspaceless %}
{% endblock field_errors %}
This is the sample code:
public function uploadAction()
{
$document = new Document();
$form = $this->createFormBuilder($document)
->add('name')
->add('file')
->getForm()
;
if ($this->getRequest()->getMethod() === 'POST') {
$form->bindRequest($this->getRequest());
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($document);
$em->flush();
$this->redirect($this->generateUrl('...'));
}
}
// Variable 'errors' is not assigned
return array('form' => $form->createView());
}
Not sure I understand. If you are following the example then $document has validation rules on it which will be tested by $form->isValid(). {{ form_errors(form) }} should output any errors.
If it is just a template customization question then you need to test for the existence of errors before trying to process them: