Symfony 2 form validation

2019-09-17 10:19发布

I want to output my form errors to screent, but I cant do that.

{{ form_errors(form) }} - in my view file is not working for me. 

Maybe I need to use $form->getErrors() if form is not valid. And then pass it to template ? I trying to serch answer but i didn get any results. Please help me.

My action(contact form), I try to return rendered page with errors:

 public function contactAction(Request $request)
{
    $form = $this->createForm(new ContactType());
    $formView = $form->createView();
    $form->handleRequest($request);

    if ($form->isValid()) {
         ....
    } else {
        $errors = $form->getErrors();

        return $this->render('VputiMainBundle:Main:contact.html.twig', array('form' => $formView, 'errors' => $errors));
    }

    return $this->render('VputiMainBundle:Main:contact.html.twig', array('form' => $formView));
}

ContactType:

 public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name', 'text', array(
        'label' => 'Ваше имя',
        'attr' => array(
            'placeholder' => 'Ваше имя',
            'class' => 'form-control',
            'name' => 'InputName',
            'id' => "InputName"
        ),
    ));
    $builder->add('email', 'email', array(
        'label' => 'Ваш e-mail',
        'attr' => array(
            'placeholder' => 'Ваш e-mail',
            'class' => "form-control",
            'id' => "InputEmail",
            'name' => "InputEmail",
        ),
    ));
    $builder->add('subject', 'text', array(
        'label' => 'Тема вопроса',
        'attr' => array(
            'placeholder' => 'Тема вопроса',
            'class' => "form-control",
            'id' => "InputSubject",
            'name' => "InputSubject",
        ),
    ));
    $builder->add('body', 'textarea', array(
        'label' => 'Вопрос',
        'attr' => array(
            'placeholder' => 'Вопрос',
            'name' => "InputMessage",
            'id' => "InputMessage",
            'class' => "form-control",
            'rows' => "5",
        ),
    ));
    $builder->add('recaptcha', 'ewz_recaptcha', array(
        'label' => 'Код с картинки',
        'attr' => array(
            'options' => array(
                'theme' => 'clean',
            )
        ),
        'mapped' => false,
        'constraints' => array(
            new True(),
        )
    ));
    $builder->add('submit', 'submit', array(
        'label' => 'Спросить',
        'attr' => array(
            'name' => "submit",
            'id' => "submit",
            'value' => "Submit",
            'class' => "btn btn-info pull-right",
        ),
    ));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $constraints = new Collection(array(
        'name' => array(
            new NotBlank(array('message' => 'Name should not be blank.')),
            new Length(array('min' => 2)),
        ),
        'email' => array(
            new NotBlank(array('message' => 'Email should not be blank.')),
            new Email(array('message' => 'Invalid email address.')),
        ),
        'subject' => array(
            new NotBlank(array('message' => 'Subject should not be blank.')),
            new Length(array('min' => 3)),
        ),
        'body' => array(
            new NotBlank(array('message' => 'Message should not be blank.')),
            new Length(array('min' => 5)),
        ),
    ));

    $resolver->setDefaults(array(
        'constraints' => $constraints,
    ));
}

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

3条回答
Lonely孤独者°
2楼-- · 2019-09-17 10:29

This is what worked for me.

Inside your bundle your bundle name/Resource/config/validation.yml you need to add the error message you want displayed for example this is what my contact form validation.yml looks like

ClickTeck\BlogBundle\Entity\Comments: // change this according to your setup
        properties:
            name:
              - NotBlank: {message: "Please provide your name"}
            email:
              - NotBlank: {message: "Please provide youe email"}
              - Email:
                          message: '"{{ value }}" is not valid.'
            comment:
              - NotBlank: {message: "Please enter your comment"}

Then inside your twig to display a message for lets say the name field,

{% if(form_errors(form.name)) %}
{{ form_errors(form.name) }}
{% endif %}

Inside your controller you need to place a check

if ($form->isValid()) {....your processing code here }

Finally in your app/config/config.yml enable validation

framework:
    validation:      { enabled: true, enable_annotations: false }
查看更多
一纸荒年 Trace。
3楼-- · 2019-09-17 10:36

As per discussion, if there is any global error, that can be displayed using {{ form_errors(form) }}

You may not have such errors. For field specific errors, you should use form_errors with form.field_name i.e. {{ form_errros(form.name) }}

Documentation

查看更多
登录 后发表回答