2011-08-07 17 views
1

件名に5文字以上必要ですが、このフィールドに1文字と送信フォームを書き込むと、エラーは表示されません。なぜ、何が間違っていますか?サーバーの検証が機能しない

WebsiteController.php:

namespace Acme\WebsiteBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 

use Acme\WebsiteBundle\Contact\ContactForm; 
use Acme\WebsiteBundle\Contact\ContactRequest; 


class WebsiteController extends Controller 
{ 
    public function contactAction(Request $request) 
    { 
     $contactRequest = new ContactRequest(); 
     $form = $this->createForm(new ContactForm(), $contactRequest); 

     if ($request->getMethod() == 'POST') 
     { 
      $form->bindRequest($request); 
      if ($form->isValid()) 
      { 
       $contactRequest->send(); 

       $this->get('session')->setFlash('notice', 'Email is ok.'); 
       $this->redirect($this->generateUrl('AcmeWebsiteBundle_homepage')); 
      } 
      else 
      { 
       $this->get('session')->setFlash('notice', 'Email is wrong.'); 
      } 
     } 

     return $this->render('AcmeWebsiteBundle:Website:contact.html.php', array('form' => $form->createView()));   
    } 
} 

ContactForm.php:

namespace Acme\WebsiteBundle\Contact; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilder; 


class ContactForm extends AbstractType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder->add('sender', 'email', array('label' => 'Email', 'trim' => true, 'required' => true, 'max_length' => 50)); 
     $builder->add('subject', 'text', array('label' => 'Temat', 'trim' => true, 'required' => true, 'max_length' => 100)); 
     $builder->add('message', 'textarea', array('label' => 'Wiadomość', 'trim' => true, 'required' => true, 'max_length' => 1000)); 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'Acme\WebsiteBundle\Contact\ContactRequest', 
     ); 
    } 

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

ContactRequest.php:

namespace Acme\WebsiteBundle\Contact; 

class ContactRequest 
{ 
    /** 
    * @validation:MinLength(5) 
    * @validation:MaxLength(100) 
    * @validation:NotBlank 
    */ 
    protected $subject; 

    /** 
    * @validation:MinLength(5) 
    * @validation:MaxLength(1000) 
    * @validation:NotBlank 
    */ 
    protected $message; 

    /** 
    * @validation:MaxLength(50) 
    * @validation:Email 
    * @validation:NotBlank 
    */ 
    protected $sender; 



    public function setSubject($subject) 
    { 
     $this->subject = $subject; 
    } 

    public function getSubject() 
    { 
     return $this->subject; 
    } 

    public function setMessage($message) 
    { 
     $this->message = $message; 
    } 

    public function getMessage() 
    { 
     return $this->message; 
    } 

    public function setSender($sender) 
    { 
     $this->sender = $sender; 
    } 

    public function getSender() 
    { 
     return $this->sender; 
    } 



    public function send() 
    { 
     //....... 
    } 
} 

答えて

1

私はあなたが教義ネイティブの検証を使用していると思います(?) symfony2では@Assertを使用する必要があります。 http://symfony.com/doc/current/book/validation.html

基本的にアサート名前空間を追加し、@Assert\

@validation:を置き換えます