gpt4 book ai didi

forms - 如何在 Symfony2 Controller 中获取表单值

转载 作者:行者123 更新时间:2023-12-03 05:14:45 25 4
gpt4 key购买 nike

我正在 Symfony2 上使用带有以下 Controller 代码的登录表单

public function loginAction(Request $request)
{
$user = new SiteUser();
$form = $this->createForm(new LoginType(), $user);


if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
$data = $form->getValues();
// Need to do something with the data here
}

return $this->render('GDSiteBundle::header.html.twig', array('form' => $form->createView()));
}

但我收到以下警告:

Warning: array_replace_recursive() [function.array-replace-recursive]: Argument #1 is not an array in \vendor\symfony\src\Symfony\Component\Form\Form.php line 593 500 Internal Server Error - ErrorException

有人可以帮助我了解哪里不正确以及如何修复它吗?谢谢。

更新:twig 文件是这样的:

<div class="form">
{{ form_errors(form) }}
<form action="{{ path('site_user_login') }}" method="POST" {{ form_enctype(form) }}>
<div class="level1">
{{ form_row(form.username) }}
<a href="javascript:void(0)" id="inscription">{% trans %}Registration{% endtrans %}</a>
</div>
<div class="level2">
{{ form_row(form.pwd_hash) }}
<div class="forget_pass"><a href="#" id="frgt">{% trans %}Forgot Password ?{% endtrans %}</a></div>
</div>
<input type="submit" class="submit" name="login" value/>
<div class="clr"></div>
</form>
</div>

这是表单类型中的函数

public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('username', 'text', array('label' => 'Username : '));
$builder->add('pwd_hash','password', array('label' => 'Password : '));
}

路线如下:

site_user_login:
pattern: /{_locale}/login
defaults: {_controller: GDSiteBundle:SiteUser:login}

最佳答案

简单地说:

$data = $form->getData();

关于forms - 如何在 Symfony2 Controller 中获取表单值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8987418/

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