Symfony2 form_errors

I think this is a simple question. About the release of errors. This is my twig file:

    <table>
        <tr>
            <td>{{ form_label(form.dueDate) }}</td>
            <td>{{ form_widget(form.dueDate) }}</td>
            <td>{{ form_errors(form.dueDate) }}</td>
        </tr>
        <tr>
            <td>{{ form_label(form.task) }}</td>
            <td>{{ form_widget(form.task) }}</td>
            <td>{{ form_errors(form.task) }}</td>
        </tr>
    </table>

Now each error displays (td using form_errors ()) as:

<ul> <li> This value must not be empty </li> </UL>

My question: I want to output the error as plain text (without ul and li).

I know there is such an example:

{% for error in errors %}
    {{ error.message }}        
{% endfor %}

But this will output errors one by one. I want to show them where there is a specific input:

<td> {{myErrorFor form.dueDate}} </TD>

Thanks so much for any help

+5
source share
4 answers

You can customize how forms are rendered by providing your own form theme with a block field_errors.

You can do this only for the current template:

{# tell the form to look for theme blocks in the current template #}
{% form_theme form _self %}

{% block field_errors %}
{% for error in errors %}
{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}<br>
{% endfor %}
{% endblock %}

{# the rest of your template... #}

config.yml:

twig:
    form: { resource: "::form_theme.html.twig" }

field_errors app/Resources/views/form_theme.html.twig, form_theme .

+13

. (twig). , , ... :

    // Controller example: 
    public function indexAction(Request $request)
    {
    $task = new \Michael\MikeBundle\Entity\Task();
    $task->setTask('Write a blog post');
    $task->setDueDate(new \DateTime('tomorrow'));

    $form = $this->createFormBuilder($task)
         ->add('task', 'text', 
                 array('attr' => array('title' => 'Enter Task')))
         ->add('dueDate', 'date', array(
             'widget' => 'single_text',
             'required' => false,
             'attr' => array('title' => 'Insert due date')))
         ->getForm();

    // If user submitted code
    if ($request->getMethod() == 'POST') {
            // Get form part from request
            $request->request->get('form');    

            // Bind request into the form
            $form->bindRequest($request);
        }

    // Pass into the view
    return array('errors' => $this->_getErrors($form), 'form' => $form->createView());
    }

    protected function _getErrors($form)
    {
    // Validate form
    $errors = $this->get('validator')->validate($form);

    // Prepare collection
    $collection = array();

    // Loop through each element of the form
    foreach ($form->getChildren() as $key => $child) {
        $collection[$key] = "";
    }

    foreach ($errors as $error) {
        $collection[str_replace("data.", "", $error->getPropertyPath())] = $error->getMessage();
    }
    return $collection;
    }

_getErrors ($ form), ( )

$errors ['task'] =

$errors ['dueDate'] = ""

:

    <table>
        <tr>
            <td>{{ form_label(form.dueDate) }}</td>
            <td>{{ form_widget(form.dueDate) }}</td>
            <td>{{ errors[form.dueDate.vars["name"]] }}</td>
        </tr>
        <tr>
            <td>{{ form_label(form.task) }}</td>
            <td>{{ form_widget(form.task) }}</td>
            <td>{{ errors[form.task.vars["name"]] }}</td>
        </tr>
    </table>

, . , .

, , .

+2

It is not possible to add comments, but I would like to update Chris Wallsmith's answer. This block is currently called form_errors, so it should be on the 3rd line {% block field_errors %}. It took me some time to investigate, I hope someone benefits from this.

+1
source

Another simple solution (tested with symfony 3):

{% for error in form.username.vars.errors %}
   {{ error.message }} <br/>
{% endfor %}

Replace "username" with the form field.

+1
source

All Articles