Simple check if form field has errors in Twig template

后端 未结 10 1633
一个人的身影
一个人的身影 2021-01-30 08:11

In Twig template I check if a field has an error like this:

{% if form.points.get(\'errors\') is not empty %}

Is there any method like:

相关标签:
10条回答
  • 2021-01-30 09:02

    better way I found, is to use this kind of code

    {% if not form.vars.valid %}
    <div class="alert alert-error">
        {{ form_errors(form) }}
    </div>
    {% endif %}
    
    0 讨论(0)
  • 2021-01-30 09:03

    If you are using symfony 4, you can check errors existence with this code

    {% if form_errors(registrationForm) %}
        <div class="alert alert-danger">        
            {{ form_errors(registrationForm) }}
        </div>
    {% endif %}
    
    0 讨论(0)
  • 2021-01-30 09:05

    This is what i use :

     <div class="form-group {{ form.brand.vars.errors|length > '' ? 'has-error' }}">
    
    0 讨论(0)
  • 2021-01-30 09:09

    i have create a twig extension to handle this: my extension

    public function hasError($string)
    {
        if(strlen($string) > 4)
            return true;
        return false;
    }
    

    i use it like this in twig

    {{ has_error(form_errors(form.username)) ? form_errors(form.username) : '' }}
    
    0 讨论(0)
提交回复
热议问题