0

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

}
2
  • 1
    Show the code of your action please. Commented Feb 9, 2012 at 15:56
  • thanks, that hint was all i needed, figured it out myself... Commented Feb 9, 2012 at 16:58

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.