为什么我的 Symfony 表单在从 Controller 呈现时不显示错误?
Why my Symfony form doesn't show errors while being rendered from Controller?
我正在使用 EasyAdminBundle 并想为管理面板创建更改密码功能。一切正常,但是有一个问题,当 RepeatedType 字段不相同以及 oldPassword 不正确时,表单不显示错误。
错误从 formType 正确发送到控制器,但是树枝不显示它们。
更改管理员密码类型
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('oldPassword', PasswordType::class, [
'constraints' => [
new NotBlank(),
new Length(['min' => 4])
],
])
->add('newPassword', RepeatedType::class, [
'type' => PasswordType::class,
'error_bubbling' => true,
'invalid_message' => 'The password fields must match.',
'first_options' => ['label' => 'New Password'],
'second_options' => ['label' => 'Repeat New Password'],
])
->add('save', SubmitType::class, [
'attr' => ['class' => 'btn btn-primary'],
])
;
}
ChangeAdminPasswordController
public function changeAdminPassword(Request $request): Response
{
$ex = new ChangeAdminPasswordDto();
$form = $this->formFactory->create(ChangeAdminPasswordType::class, $ex);
$view = $form->createView();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (($this->changeAdminPassword)($this->getUser(), $form->getData())) {
return $this->redirectToRoute('admin_dashboard');
}
$form->addError(new FormError('Current password is not correct'));
}
return $this->render('bundles/EasyAdminBundle/changeAdminPassword.html.twig', [
'form' => $view,
]);
}
changAdminPassword.html.twig
{% extends '@EasyAdmin/layout.html.twig' %}
{% block content_title %}
<div class="content-header-title">
<h1 class="title">
Change password
</h1>
</div>
{% endblock %}
{% block main %}
{{ form_errors(form) }}
{{ form_start(form) }}
<div class="col-md6 col-xxl-5">
<div class="field-text form-group">
{{ form_label(form.oldPassword) }}
<div class="form-widget">
{{ form_widget(form.oldPassword, { 'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="field-text form-group">
{{ form_label(form.newPassword.first) }}
<div class="form-widget">
{{ form_widget(form.newPassword.first, { 'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="field-text form-group">
{{ form_label(form.newPassword.second) }}
<div class="form-widget">
{{ form_widget(form.newPassword.second, { 'attr': {'class': 'form-control'}}) }}
</div>
</div>
</div>
<div class="page-actions">
{{ form_row(form.save) }}
</div>
{{ form_end(form) }}
{% endblock %}
您正在此处提交之前创建表单视图 $view = $form->createView();
。任何验证后都不会呈现任何错误消息...
改变这个:
return $this->render('bundles/EasyAdminBundle/changeAdminPassword.html.twig',
[
'form' => $view,
]);
为此:
return $this->render('bundles/EasyAdminBundle/changeAdminPassword.html.twig',
[
'form' => $form->createView(),
]);
我正在使用 EasyAdminBundle 并想为管理面板创建更改密码功能。一切正常,但是有一个问题,当 RepeatedType 字段不相同以及 oldPassword 不正确时,表单不显示错误。 错误从 formType 正确发送到控制器,但是树枝不显示它们。 更改管理员密码类型
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('oldPassword', PasswordType::class, [
'constraints' => [
new NotBlank(),
new Length(['min' => 4])
],
])
->add('newPassword', RepeatedType::class, [
'type' => PasswordType::class,
'error_bubbling' => true,
'invalid_message' => 'The password fields must match.',
'first_options' => ['label' => 'New Password'],
'second_options' => ['label' => 'Repeat New Password'],
])
->add('save', SubmitType::class, [
'attr' => ['class' => 'btn btn-primary'],
])
;
}
ChangeAdminPasswordController
public function changeAdminPassword(Request $request): Response
{
$ex = new ChangeAdminPasswordDto();
$form = $this->formFactory->create(ChangeAdminPasswordType::class, $ex);
$view = $form->createView();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (($this->changeAdminPassword)($this->getUser(), $form->getData())) {
return $this->redirectToRoute('admin_dashboard');
}
$form->addError(new FormError('Current password is not correct'));
}
return $this->render('bundles/EasyAdminBundle/changeAdminPassword.html.twig', [
'form' => $view,
]);
}
changAdminPassword.html.twig
{% extends '@EasyAdmin/layout.html.twig' %}
{% block content_title %}
<div class="content-header-title">
<h1 class="title">
Change password
</h1>
</div>
{% endblock %}
{% block main %}
{{ form_errors(form) }}
{{ form_start(form) }}
<div class="col-md6 col-xxl-5">
<div class="field-text form-group">
{{ form_label(form.oldPassword) }}
<div class="form-widget">
{{ form_widget(form.oldPassword, { 'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="field-text form-group">
{{ form_label(form.newPassword.first) }}
<div class="form-widget">
{{ form_widget(form.newPassword.first, { 'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="field-text form-group">
{{ form_label(form.newPassword.second) }}
<div class="form-widget">
{{ form_widget(form.newPassword.second, { 'attr': {'class': 'form-control'}}) }}
</div>
</div>
</div>
<div class="page-actions">
{{ form_row(form.save) }}
</div>
{{ form_end(form) }}
{% endblock %}
您正在此处提交之前创建表单视图 $view = $form->createView();
。任何验证后都不会呈现任何错误消息...
改变这个:
return $this->render('bundles/EasyAdminBundle/changeAdminPassword.html.twig',
[
'form' => $view,
]);
为此:
return $this->render('bundles/EasyAdminBundle/changeAdminPassword.html.twig',
[
'form' => $form->createView(),
]);