Php Symfony 4-isValid始终返回false
我正在尝试使用Php Symfony 4-isValid始终返回false,php,symfony,symfony-forms,symfony4,Php,Symfony,Symfony Forms,Symfony4,我正在尝试使用$form->isValid()验证我的表单。但即使我的表单是正确的,它也会返回false。我已经尝试使用$form->getErrors(true)转储错误,但是请求超时 mycreatecontroller.php: class CreateController extends Controller { /** * @Method({"POST"}) * @Route("/api/v1/matches", name="api_v1_matches_c
$form->isValid()
验证我的表单。但即使我的表单是正确的,它也会返回false。我已经尝试使用$form->getErrors(true)
转储错误,但是请求超时
mycreatecontroller.php:
class CreateController extends Controller
{
/**
* @Method({"POST"})
* @Route("/api/v1/matches", name="api_v1_matches_create")
*/
public function index(Request $request, EntityManagerInterface $em): JsonResponse
{
$data = json_decode($request->getContent(), true);
$match = new Match();
$form = $this->createForm(MatchFormType::class, $match);
$form->submit($data);
if ($form->isValid()) {
$em->persist($match);
$em->flush();
return new JsonResponse(null, 201);
} else {
return new JsonResponse(null, 400);
}
}
}
My Form.php
class MatchFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'heroes',
EntityType::class,
[
'class' => Hero::class,
]
)
->add(
'season',
EntityType::class,
[
'class' => Season::class,
]
)
->add(
'map',
EntityType::class,
[
'class' => Map::class,
]
);
}
public function getName(): string
{
return 'match';
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Match::class,
]);
}
}
JSON发布
{
"map": 1,
"heroes": [
1,
2
],
"season": 1
}
提前感谢您的帮助 我通过在我的heroes条目中添加
'multiple'=>true
来修复它,因此表单知道它是一个数组,并通过禁用CSRF保护('CSRF\u protection'=>false
作为$resolver中的参数)来修复它。我相信您可能希望遵循此处文档中描述的实践
在您的案例中,将文章示例分为以下步骤:
XDebug
,并在您的请求遇到问题时查看正在转储的调用跟踪?