简体   繁体   中英

I cannot get my form to be valid or be submitted. isValid() returns false

I cannot get my form to be valid or be submitted. isValid() returns false. Using PHP , symfony2 with entities - anotations, using var_dump( $form->getErrorsAsString() ); which returns no errors. symfony2 isValid() returns false without errors

my code:

<?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(),
        ));
    }
}

my entities:

<?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;
    }
}

My View :

{% 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 %}

Perhaps you might consider dumping $form->getErrors() instead.

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);

My guess is that you might need to set your PHP error level to E_ALL.

  1. If your Form is not submitted in first place, look into html source code, if you have all components, especially the form-tag there.
  2. When you want to submit data, ensure that this is valid (eg especially, when you have an email field.). The form should be marked red, if there is smt wrong.
  3. When this problem persists, I would suggest to separate the logic into two methods. First only displays the form, second handles the action and/or error. Of course you then have $formbuilder->setAction($this->generateUrl('testform'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM