Forms 不能对未绑定-Symfony的窗体调用isValid()
我试图使用Symfony创建表单,但收到错误: 不能对未绑定的窗体调用isValid()Forms 不能对未绑定-Symfony的窗体调用isValid(),forms,symfony,doctrine,Forms,Symfony,Doctrine,我试图使用Symfony创建表单,但收到错误: 不能对未绑定的窗体调用isValid() 我有一个用户类,其注释和条令保存在DB中。类用户具有使用条令生成的getter和setter 我有UserTye.php: <?php namespace Iftodi\DesignBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; class UserType
<?php
namespace Iftodi\DesignBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name');
$builder->add('username');
$builder->add('email','email');
$builder->add('password','password');
}
public function getName()
{
return "userRegister";
}
}
?>
只有在有post请求时才绑定表单,但始终在表单上调用isValid。正如这条信息所说:如果它没有绑定,你就不能。按如下方式重新组织代码:
if ("POST" === $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
}
}
只有在有post请求时才绑定表单,但始终在表单上调用isValid。正如这条信息所说:如果它没有绑定,你就不能。按如下方式重新组织代码:
if ("POST" === $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
}
}
梅林的回答是正确的 下面是更新后的代码片段:用于Symfony 2.1+
if ('POST' === $request->getMethod()) {
$form->bind($request);
if ($form->isValid()) {
// do something with $form
}
}
参考:
如果您不在控制器中,而是在细枝扩展中,则可以通过bundle(注意Symfony 2.4版本)中services.yml中的服务注入声明传递请求堆栈
参考:
那么在你的分机里
namespace YourBundle;
use Symfony\Component\HttpFoundation\RequestStack;
class YourTwigExtension extends \Twig_Extension
{
private $request;
public function __construct(RequestStack $request_stack)
{
$tmp_request = $request_stack->getCurrentRequest();
if ($tmp_request)
{
$this->request = $tmp_request;
}
}
// add your filter/function/extension name declarations ...
public function yourFancyMethod()
{
if($this->request)
{
if ('POST' === $this->request->getMethod())
{
$form->bind($this->request);
if ($form->isValid())
{
// do something with $form
}
}
}
}
}
梅林的回答是正确的 下面是更新后的代码片段:用于Symfony 2.1+
if ('POST' === $request->getMethod()) {
$form->bind($request);
if ($form->isValid()) {
// do something with $form
}
}
参考:
如果您不在控制器中,而是在细枝扩展中,则可以通过bundle(注意Symfony 2.4版本)中services.yml中的服务注入声明传递请求堆栈
参考:
那么在你的分机里
namespace YourBundle;
use Symfony\Component\HttpFoundation\RequestStack;
class YourTwigExtension extends \Twig_Extension
{
private $request;
public function __construct(RequestStack $request_stack)
{
$tmp_request = $request_stack->getCurrentRequest();
if ($tmp_request)
{
$this->request = $tmp_request;
}
}
// add your filter/function/extension name declarations ...
public function yourFancyMethod()
{
if($this->request)
{
if ('POST' === $this->request->getMethod())
{
$form->bind($this->request);
if ($form->isValid())
{
// do something with $form
}
}
}
}
}
你好@DanIftodi,欢迎来到Stackoverflow。如果这个答案解决了你的问题,你可以!你好@DanIftodi,欢迎来到Stackoverflow。如果这个答案解决了你的问题,你可以!