对象“Symfony \ Component \ Form \ FormView”的方法xyz不存在

时间:2016-04-18 20:37:38

标签: php forms symfony

当我将表单移动到表单类时,显示我的新表单时出现问题,我真的不知道如何处理此问题

我使用Symfony 3.0.4。我认为我从Symfony文档和StackOverFlow的提示中搜索了所有内容,但很多这些数据来自Symfony 2.x。

  

对象“Symfony \ Component \ Form \ FormView”的方法“保存”没有   存在于FlexPMUsersBundle中:默认值:register.html.twig,第8行500   内部服务器错误 - Twig_Error_Runtime

我有4个文件:

的src / FlexPM / UsersBundle /资源/视图/默认/ register.html.twig

{% extends 'base.html.twig' %}

{% block body %}
<div>
    Test
    {{ form_start(formRegister) }}
        {{ form_errors(formRegister) }}

        {{ form_row(formRegister.save) }}
    {{ form_end(formRegister) }}

</div>
{% endblock %}

的src / FlexPM / UsersBundle /控制器/ DefaultController.php

<?php

namespace FlexPM\UsersBundle\Controller;

use FlexPM\UsersBundle\Form\Type\UserRegister;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use FlexPM\UsersBundle\Entity\Users;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class DefaultController extends Controller
{

    /**
     * @Route("/register")
     */
    public function registerAction(Request $request)
    {
        $newUser = new Users();
        $newUser->setEmail('abc@o2.com');
        $newUser->setPassword('1234');
        $newUser->setPermissions(1);
        $newUser->setCreateDate(time());

        $formRegister = $this->createForm(UserRegister::class, $newUser);

        $formRegister->handleRequest($request);

        if($formRegister->isSubmitted() && $formRegister->isValid()){
            return $this->redirectToRoute('homepage');
        }

        return $this->render('FlexPMUsersBundle:Default:register.html.twig', array(
            'formRegister' => $formRegister->createView(),
        ));
    }
}

的src / FlexPM / UsersBundle /窗体/类型/ UserRegister.php

<?php
// src/FlexPM/Form/Type/UserRegister.php
namespace FlexPM\UsersBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UserRegister extends AbstractType
{

    public function registerForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('Save', SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FlexPM\UsersBundle\Entity\Users',
        ));
    }

}

的src / FlexPM / UsersBundle /实体/ Users.php

<?php

namespace FlexPM\UsersBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

/**
 * Users
 */
class Users
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     * @Assert\NotBlank()
     * @Assert\Email(
     *     message = "The email '{{ value }}' is not a valid email.",
     *     checkMX = true
     * )
     */
    private $email;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     * @Assert\NotBlank()
     * @Assert\Length(
     *      min = 8,
     *      max = 50,
     *      minMessage = "Your password must be at least {{ limit }} characters long",
     *      maxMessage = "Your password cannot be longer than {{ limit }} characters"
     * )
     */
    private $password;

    /**
     * @var integer
     */
    private $permissions;

    /**
     * @var \DateTime
     */
    private $createDate;

    /**
     * @var \DateTime
     */
    private $lastLogin;

    /**
     * @var string
     */
    private $userCookie;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set email
     *
     * @param string $email
     *
     * @return Users
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Users
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set password
     *
     * @param string $password
     *
     * @return Users
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Get password
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set permissions
     *
     * @param integer $permissions
     *
     * @return Users
     */
    public function setPermissions($permissions)
    {
        $this->permissions = $permissions;

        return $this;
    }

    /**
     * Get permissions
     *
     * @return integer
     */
    public function getPermissions()
    {
        return $this->permissions;
    }

    /**
     * Set createDate
     *
     * @param \DateTime $createDate
     *
     * @return Users
     */
    public function setCreateDate($createDate)
    {
        $this->createDate = $createDate;

        return $this;
    }

    /**
     * Get createDate
     *
     * @return \DateTime
     */
    public function getCreateDate()
    {
        return $this->createDate;
    }

    /**
     * Set lastLogin
     *
     * @param \DateTime $lastLogin
     *
     * @return Users
     */
    public function setLastLogin($lastLogin)
    {
        $this->lastLogin = $lastLogin;

        return $this;
    }

    /**
     * Get lastLogin
     *
     * @return \DateTime
     */
    public function getLastLogin()
    {
        return $this->lastLogin;
    }

    /**
     * Set userCookie
     *
     * @param string $userCookie
     *
     * @return Users
     */
    public function setUserCookie($userCookie)
    {
        $this->userCookie = $userCookie;

        return $this;
    }

    /**
     * Get userCookie
     *
     * @return string
     */
    public function getUserCookie()
    {
        return $this->userCookie;
    }
}

你知道我该怎么办?

---解决方案---:

的src / FlexPM / UsersBundle /窗体/类型/ UserRegister.php

<?php
// src/FlexPM/Form/Type/UserRegister.php
namespace FlexPM\UsersBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UserRegister extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('save', SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FlexPM\UsersBundle\Entity\Users',
        ));
    }

}

1 个答案:

答案 0 :(得分:1)

registerForm函数不应该是buildForm吗?我不认为registerForm被调用,因为你试图手动输出按钮{{ form_row(formRegister.save) }},它会抛出你收到的错误。