Configuring File Upload Error Message
Using the guide http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html I am trying to customize the error message but I have a problem: the variable errors
is not since we are not validating the entity and not calling $this->get('validator')->validate($entity)
.
{% block field_errors %}
{% spaceless %}
{# errors is undefined here #}
{% endspaceless %}
{% endblock field_errors %}
This is some 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());
}
+3
source to share
1 answer
I'm not sure I understand. If you are following the example, there are validation rules in $ document that will be validated using $ form-> isValid (). {{form_errors (form)}} should output any errors.
If it's just a matter of customizing the template, you need to check for errors before trying to handle them:
{% block field_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<span style="color:red">
{% for error in errors %}
{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}<br />
{% endfor %}
{% endif %}
{% endspaceless %}
{% endblock field_errors %}
+1
source to share