gpt4 book ai didi

php - 在 twig symfony 中显示所有表单错误

转载 作者:行者123 更新时间:2023-12-05 07:41:17 25 4
gpt4 key购买 nike

我有一个包含集合类型(字段 col)的表单。

在 collectiontype 的字段上(称之为 a),我有返回错误的验证器。

我尝试使用 {{form_errors(form)}}{{form_errors(form.col)}} 但它们不起作用...

最佳答案

你可以使用FormErrorsSerializer获取所有错误并将其作为 JSON 数组返回,此代码片段来 self 正在处理的项目

function addOwnership(Request $request)
{
$id = $request->get('land_id');
$land = $this->getDoctrine()->getRepository('DamanBundleCoreBundle:Land')->find($id);
$ownership = new OwnershipHistory();
$ownership_form = $this->createForm('Daman\Bundle\CoreBundle\Form\OwnershipType', $ownership);
$ownership_form->handleRequest($request);

if ($ownership_form->isSubmitted()) {
if ($ownership_form->isValid()) {

$em = $this->getDoctrine()->getManager();
$ownership->setLandId($land);
$em->persist($ownership);
$em->flush();
$response = array('success' => true,
'msg' => $this->get('translator')->trans('data_has_been_successfully_added'),
'id' => $ownership->getId(),
'action' => 'refresh'
);

} else {
$errors = $this->get('form_serializer')->serializeFormErrors($ownership_form, true, false);
$response = array('success' => false, 'msg' => $this->get('translator')->trans('error_exist_in_the_form'), 'errors' => $errors);

}
$jsonResponse = new JsonResponse($response);
return $jsonResponse;

}


}//end function

关于php - 在 twig symfony 中显示所有表单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45456122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com