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';
}