我的symfony4中的表单有问题,当我点击提交按钮时它没有提交(它没有通过isSubmitted
条件)
这是我的表格
<?php
namespace App\Form\Backend\Team;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
/**
* Class AddTeamForm
* @package App\Form\Backend\Team
*/
class TeamForm extends AbstractType
{
const NAME = 'name';
const WEBSITE = 'website';
const IRC = 'irc';
const DISCORD = 'discord';
const TWITTER = 'twitter';
const FACEBOOK = 'facebook';
const HIDDEN = 'hidden';
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id', HiddenType::class)
->add(self::NAME, TextType::class, [
'required' => true,
'label' => 'admin.team.form.name'
])
->add(self::WEBSITE, TextType::class, [
'required' => false,
'label' => 'admin.team.form.website'
])
->add(self::IRC, TextType::class, [
'required' => false,
'label' => 'admin.team.form.irc'
])
->add(self::DISCORD, TextType::class, [
'required' => false,
'label' => 'admin.team.form.discord'
])
->add(self::TWITTER, TextType::class, [
'required' => false,
'label' => 'admin.team.form.twitter'
])
->add(self::FACEBOOK, TextType::class, [
'required' => false,
'label' => 'admin.team.form.facebook'
])
->add(self::HIDDEN, CheckboxType::class, [
'required' => false,
'label' => 'admin.team.form.hidden'
]);
}
}
这是我的行动
/**
* @Route("/add", name="add")
*/
public function add(Request $request)
{
$form = $this->createForm(TeamForm::class);
$form->handleRequest($request);
if ($form->isSubmitted()) {
var_dump('submitted');
if($form->isValid())
{
} else {
var_dump('non vlide');exit;
}
$team = $form->getData();
}
return $this->render('backend/team/add.html.twig', [
'form' => $form->createView(),
]);
}
和我的观点
{{ form_start(form, {attr: {class: 'ui form inverted', novalidate:'novalidate', 'action': url('admin_team_add')} }) }}
{{ form_errors(form) }}
<div class="ui grid two column">
<div class="row">
<div class="twelve wide column">
{{ form_row(form.name) }}
{{ form_row(form.website) }}
{{ form_row(form.irc) }}
{{ form_row(form.discord) }}
{{ form_row(form.twitter) }}
{{ form_row(form.facebook) }}
<div class="field">
<div class="ui checkbox">
{{ form_widget(form.hidden) }}
{{ form_label(form.hidden) }}
{{ form_errors(form.hidden) }}
</div>
</div>
<input type="submit" class="ui button green" value="{{ 'admin.team.form.btn-create' }}"/>
</div>
</div>
</div>
{{ form_end(form) }}
就像我说的,当我点击我的提交按钮时,不会显示var_dump'提交'。页面刷新但没有任何反应,就像正常的页面刷新一样。
我必须错过一些东西,但无法想象......
答案 0 :(得分:0)
您可以检查您的路线名称是否存在:
php bin/console debug:router admin_team_add
如果不存在,请将您的视图操作更改为:
{{ form_start(form, {attr: {class: 'ui form inverted', novalidate:'novalidate', 'action': url('add')} }) }}
或将您的控制器路由名称更改为:
/**
* @Route("/add", name="admin_team_add")
*/
public function add(Request $request)
{