我有一个twig页面,它呈现我控制器中定义的表单:
$app->post('/thepath', function(Request $request) use ($app) {
$app['request'] = $request;
$token = $app['security.token_storage']->getToken();
if (null !== $token) {
$user = $token->getUser();
$form = $app['form.factory']->createBuilder(FormType::class)
->add('firstname', TextType::class, [
'required' => true,
])
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
// The code I want to run.
}
return $app['twig']->render('file.twig', [
'form' => $form->createView(),
]);
}
})->bind('thepath');
但是当我提交表格时
{{ form_start(form) }}
{{ form_label(form.firstname) }}<br />
{{ form_widget(form.firstname) }}<br />
{{ form_errors(form.firstname) }}
<input type="submit" value="Valider" />
{{ form_end(form) }}
似乎表单未提交或无效,因为if($ form-&gt; isSubmitted()&amp;&amp; $ form-&gt; isValid()){子句永远不会是真的!
我不明白为什么......
编辑:表单已提交但无效。因为
if($form->isSubmitted()) { // is true
if($form->isValid()) { // is false