stoefln
stoefln

Reputation: 14605

Symfony2: Why does my form show errors even before submit?

I created a FormType for a registration form. The validation work like it should. Strange thing is: The errors get printed right away. When displayed (on load of the page), the form tells me right away that some fields are not valid, although I haven't started to fill in the fields at that point.

my formtype class:

<?php
namespace MyBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\MaxLength;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use MyBundle\Validation\Constraint\Unique;
use MyBundle\Validation\Constraint\InvitationCode;
use MyBundle\Validation\Constraint\Username;

class RegistrationFormType extends AbstractType
{
    /**
     *
     * @var FormBuilder
     */
    private $builder;

    public function buildForm(FormBuilder $builder, array $options)
    {
        $this->builder = $builder;
        $this->builder
            ->add('code','text', array(
                'label' => 'Einladungscode'
            ))->add('username','text',array(
                'label' => 'Benutzername',
            ))
            ->add('email', 'email',array(
                'label' => 'E-Mail'
            ))
            ->add('plainPassword', 'repeated', array('type' => 'password',
                'first_name'=>'Passwort',
                'second_name'=> 'Passwort wiederholen',
                )
            );
    }

    public function showRegistrationFields(){


    }
    public function getDefaultOptions(array $options)
    {
        $collectionConstraint = new Collection(array(
            'email' => array(
                    new NotBlank(),
                    new Unique(array(
                        'entityName' => 'AjadoEventHubBundle:User',
                        'propertyName' => 'email')),
                    new Email(array(
                            'message' => 'Ungültige E-Mail Adresse',
                        )),

                ),
            'username' => array(
                    new NotBlank(),
                    new Unique(array(
                        'entityName' => 'AjadoEventHubBundle:User',
                        'propertyName' => 'username')),
                    new Username(),
                    new MinLength(array('limit' => 5)),
                    new MaxLength(array('limit' => 40)),
                ),
            'code' => array(
                    new MaxLength(array('limit'=>200)),
                    new InvitationCode(),
                ),
            'plainPassword' => array(
                    new MaxLength(array('limit'=>20)),
                    new MinLength(array('limit' => 5))
                ),
        ));

        return array(
            'csrf_protection' => false,
            'validation_constraint' => $collectionConstraint,
        );
    }

    public function getName()
    {
        return 'registration';
    }

}

Upvotes: 0

Views: 1236

Answers (1)

stoefln
stoefln

Reputation: 14605

I changed

$form->bindRequest($this->getRequest());
if ($this->getRequest()->getMethod() == 'POST' && $form->isValid()) {

to

if ($this->getRequest()->getMethod() == 'POST') {
            $form->bindRequest($this->getRequest());
            if($form->isValid()){

Thanks @meze for the hint!

Upvotes: 2

Related Questions