我不能让我的表格有效或被提交。 isValid()返回false

时间:2015-06-30 14:52:27

标签: php forms symfony

我无法让我的表格生效或提交。 isValid()返回false。使用PHPsymfony2使用实体 - anotations,使用var_dump( $form->getErrorsAsString() );返回无错误。symfony2 isValid() returns false without errors

我的代码:

<?php

namespace Fishing\UsersBundle\Controller;

use AppBundle\Entity\Users;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class GetController extends Controller
{
    public function indexAction(Request $request)
    {
        $users = new Users();

        $form =$this->createFormBuilder($users)
            ->setMethod('POST')
            ->add('firstName', 'text')
            ->add('lastName', 'text')
            ->add('email', 'email')
            ->add('plainPassword','password')
            ->add('save', 'submit', array('label' => 'Create User'))
            ->getForm();

        $form->handleRequest($request); 

        if ($form->isValid()) {
        // perform some action...
            /*$em = $this->getDoctrine()->getManager();

            $em->persist($users);
            $em->flush();
            */
        return new Response('isValid!');
        } else {

        var_dump( $form->getErrorsAsString() );
        //die;
        return new Response('notvalid');
        }

        return $this->render('default/userForm.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

我的实体:

<?php
// src/AppBundle/Entity/Users.php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;


/**
 * @ORM\Entity
 * @UniqueEntity(fields="email", message="Email already taken")
 * 
 */
class Users
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Assert\NotBlank()
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     * @Assert\NotBlank()
     */
    protected $firstName;

    /**
     * @ORM\Column(type="string", length=100)
     * @Assert\NotBlank()
     */
    protected $lastName;
    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     * @Assert\Email()
     */
    protected $email;
    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     * @Assert\Length(max = 4096)
     */
    protected $plainPassword;

    /**
     * @ORM\Column(type="datetime", name="created_at")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Assert\NotBlank()
     */
    protected $createdat;

    /**
     * @ORM\Column(type="datetime", name="updated_at")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Assert\NotBlank()
     */
    protected $updatedat;

    public function getFirstName()
    {
        return $this->firstName;
    }

    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;
    }

    public function getLastName()
    {
        return $this->lastName;
    }

    public function setLastName($lastName)
    {
        $this->lastName = $lastName;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function setPlainPassword($password)
    {
        $this->plainPassword = $password;
    }

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

        return $this;
    }

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

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

        return $this;
    }

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

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

我的观点:

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

{% block title %}User Input{% endblock %}

{% block body %}
    New User Input Form!
    {{ form_start(form) }}
        {{ form_widget(form) }}
        <br>
    {{ form_end(form) }}

{% endblock %}

2 个答案:

答案 0 :(得分:0)

也许您可以考虑转储$form->getErrors()

looking at the source

function getErrorsAsString($level = 0) {
    @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Use (string) Form::getErrors(true, false) instead.', E_USER_DEPRECATED);

我的猜测是您可能需要将PHP错误级别设置为E_ALL。

答案 1 :(得分:0)

  1. 如果您的表单未首先提交,请查看html源代码,如果您有所有组件,尤其是那里的表单标记。
  2. 如果要提交数据,请确保其有效(例如,当您有电子邮件字段时)。如果有错误,表格应标记为红色。
  3. 当这个问题持续存在时,我建议将逻辑分为两种方法。首先只显示表单,第二个处理动作和/或错误。当然,你有$formbuilder->setAction($this->generateUrl('testform'))