form_errors(form)在Symfony Twig中没有显示任何错误

时间:2015-11-24 08:44:51

标签: php symfony twig

我只是不知道为什么form_errors(form)不会打印任何错误?

这是我的twig文件

<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register signup-form" novalidate>                        
                        <div class="errors">
                             {{ form_errors(form) }}   
                        </div>
                        <div class="form-input-group">
                            <i class="fa fa-check"></i> {{ form_widget(form.firstName, { 'attr': {'class': 'form-control'} } ) }}
                        </div>
                        <div class="form-input-group">
                            <i class="fa fa-check"></i> {{ form_widget(form.lastName, { 'attr': {'class': 'form-control'} } ) }}
                        </div>
                       <div class="form-input-group">
                            <i class="fa fa-check"></i> {{ form_widget(form.contactNumber, { 'attr': {'class': 'form-control'} } ) }}
                        </div>
                        <div class="form-input-group input-append date" id="datepicker">                        
                                <i class="fa fa-calendar"></i>
                                {{ form_widget(form.birthday, 
                                    {attr: { 
                                        'data-format': "dd/MM/yyyy hh:mm:ss" },
                                    }) 
                                }}
                                <span class="add-on"> 
                                    <i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
                                </span>

                        </div>
                        <div class="form-input-group">
                            <i class="fa fa-venus-mars"></i>
                            <div class="gender radio">                                    
                                <label class="radio-inline">{{ form_widget(form.gender[0]) }} &nbsp; Male </label>        
                                <label class="radio-inline">{{ form_widget(form.gender[1]) }} &nbsp; Female </label>                   
                            </div>
                        </div>
                        <div class="form-input-group">
                            <i class="fa fa-check"></i> {{ form_widget(form.username, { 'attr': {'class': 'form-control', 'placeholder': 'Enter username'} } ) }}
                        </div>
                        <div class="form-input-group">
                            <i class="fa fa-check"></i> {{ form_widget(form.email, { 'attr': {'class': 'form-control', 'placeholder': 'Enter email address'} } ) }}
                        </div>
                        <div class="form-input-group">
                            <i class="fa fa-lock"></i> {{ form_widget(form.plainPassword.first, { 'attr': {'class': 'form-control', 'placeholder': 'Enter password'} } ) }}
                        </div>
                         <div class="form-input-group">
                            <i class="fa fa-lock"></i> {{ form_widget(form.plainPassword.second, { 'attr': {'class': 'form-control', 'placeholder': 'Repeat password'} } ) }}
                        </div>
                        <div class="row">
                             <div class="col-md-6 col-md-offset-3">
                                 <button type="submit" class="btn-fill sign-up-btn">Register</button>
                            </div>
                        </div>   
                        {{ form_rest(form) }} 
                    </form>

这是我的表单类型:

   class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        parent::buildForm($builder, $options);

        $gender = array(
            1 => 'Male',
            0 => 'Female'
        );

        $builder
        ->add('username', 'text' , array('attr'=> array('placeholder'=>'Enter username')))
        ->add('firstName', 'text', array('attr'=> array('placeholder'=>'Enter first name')))
        ->add('lastName', 'text',  array('attr'=> array('placeholder'=>'Enter last name')))
        ->add('birthday', 'datetime', array(
            'attr' => array('placeholder' => 'Pick your birthday'),        
            'widget' => 'single_text', 
            'html5' => false,          
        ))
        ->add('gender', 'choice', array(
            'attr' => array('class' => 'form-control'),        
            'choices' => $gender,
            'data' => 1,
            'expanded' => true,
        ))
        ->add('contactNumber','text' , array('attr'=> array('placeholder'=>'Enter contact number')));
    }

    public function getParent()
    {
        return 'fos_user_registration'; 
    }

    public function getName()
    {
        return 'app_user_registration';
    }

我知道有错误,因为如果它出现时我会像这样单独渲染它们:

form_errors(form.firstName);

顺便说一句,我使用的是2.7版本。

2 个答案:

答案 0 :(得分:3)

只有当error_bubbling为此字段设置为true时,子字段的错误才会传递给其父字段。

答案 1 :(得分:1)

您需要为每个表单元素添加约束。

示例:

use Symfony\Component\Validator\Constraints as Assert;

// ...

$builder->add('username', 'text', array(
    // ...
    'constraints' => array(
        new Assert\Length(array(
            'min' => 4
        )
    ),
    // ...
);

如果提交的用户名字段长度小于4,则会显示错误。

检查此link是否有其他验证限制。