我想将表单错误输出到screent,但我不能这样做。
{{ form_errors(form) }} - in my view file is not working for me.
如果表单无效,也许我需要使用$ form-> getErrors()。然后把它传递给模板? 我试图回答但我没有得到任何结果。请帮帮我。
我的操作(联系表单),我尝试返回带有错误的呈现页面:
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';
}
答案 0 :(得分:1)
答案 1 :(得分:1)
根据讨论,如果有任何全局错误,可以使用{{ form_errors(form) }}
您可能没有此类错误。对于特定于字段的错误,您应该form_errors
使用form.field_name
,即{{ form_errros(form.name) }}
<强> Documentation 强>
答案 2 :(得分:1)
这对我有用。
在您的软件包your bundle name/Resource/config/validation.yml
中,您需要添加要显示的错误消息,例如,这是我的联系表单validation.yml
的样子
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"}
然后在你的树枝里面显示一条消息,让我们说出名字字段,
{% if(form_errors(form.name)) %}
{{ form_errors(form.name) }}
{% endif %}
在控制器内部,您需要进行检查
if ($form->isValid()) {....your processing code here }
最后在您的app/config/config.yml
启用验证
framework:
validation: { enabled: true, enable_annotations: false }