2012-02-09 3 views
0

등록 양식에 대한 FormType을 만들었습니다. 그것과 같은 검증 작업이 필요합니다. 이상한 것은 : 오류가 즉시 인쇄됩니다. 페이지로드시 폼이 표시되면 해당 필드를 채우기 시작하지 않았지만 양식에서 일부 필드가 유효하지 않음을 즉시 알립니다.Symfony2 : 제출하기 전에도 내 양식에 오류가 표시되는 이유는 무엇입니까?

내 formtype 클래스 :

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

} 
+1

조치 코드를 보여주십시오. – meze

+0

감사합니다. 그 힌트는 내가 필요로하는 것이고, 알아 냈습니다. – stoefln

답변

2

나는 힌트에 대한 @meze

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

감사를

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

변경!

관련 문제