当我刷新页面内容然后是持久对象时,我有这个错误。
代码处理程序:使用句柄创建表单时的内容函数发布:
public function post(array $parameters, bool $submitted = false, array $options = [])
{
$recruiter = $this->formHandler->handle(
$this->factory->create(),
$parameters,
Request::METHOD_POST,
$submitted,
$options
);
if ($recruiter instanceof Recruiter) {
$this->repository->save($recruiter);
return $recruiter;
}
}
代码控制器:使用函数post作为处理程序,我使用try和catch:
/**
* @Template()
* @Route("/register", name="register_recruiter")
*
* @param Request $request
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function registerAction(Request $request)
{
try {
$quiz = $this->getHandler()->post($request->request->all());
return $this->redirectToRoute('recruiter_quizs_index');
}
catch (InvalidFormException $e) {
return [
'form' => $e->getForm()->createView(),
'edit' => false,
];
}
代码树枝内容形式:
{{ form(form) }}
创建表单的函数句柄:
public function handle($object, array $parameters, string $method, bool $submited = false, array $options = [])
{
$options = array_replace_recursive([
'method' => $method,
'csrf_protection' => false,
], $options);
$form = $this->formFactory->create(get_class($this->formType), $object, $options);
if (!$submited) {
throw new InvalidFormException($form);
}
$form->submit($parameters, 'PATCH' !== $method);
if (!$form->isValid()) {
throw new InvalidFormException($form);
}
return $form->getData();
}
答案 0 :(得分:1)
问题是你没有为第二个参数$submitted
提供一个值,如果方法是POST,PUT或PATCH,它应该是true
。
解决这个问题的简单方法是检查操作中的方法并提供布尔参数
/**
* @Template()
* @Route("/register", name="register_recruiter")
*
* @param Request $request
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function registerAction(Request $request)
{
try {
$submitted = ($request->isMethod('PATCH') || $request->isMethod('POST') || $request->isMethod('PUT'));
$quiz = $this->getHandler()->post($request->request->all(), $submitted);
return $this->redirectToRoute('recruiter_quizs_index');
}
catch (InvalidFormException $e) {
return [
'form' => $e->getForm()->createView(),
'edit' => false,
];
}
虽然我认为这是一个非常糟糕的解决方案。您应该为每个HTTP方法创建一个操作。使用GET显示表单,使用POST / PUT / PATCH提交。将提交路由放在GET路由之前,因此它将首先匹配:
/**
* submit the recruiter form (on PATH, POST, PUT)
*
* @Template()
* @Route("/register", name="register_recruiter_submit", methods={"PATCH","POST","PUT"})
*
* @param Request $request
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function registerSubmitAction(Request $request)
{
try {
$quiz = $this->getHandler()->post($request->request->all(), true);
return $this->redirectToRoute('recruiter_quizs_index');
}
catch (InvalidFormException $e) {
return [
'form' => $e->getForm()->createView(),
'edit' => false,
];
}
}
/**
* show the recruiter form (route will match if method is not PATCH/PUT/POST
*
* @Template()
* @Route("/register", name="register_recruiter_form")
*
* @param Request $request
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function registerFormAction(Request $request)
{
return [
'form' => $this->formHandler->createView(),
'edit' => false,
];
}