2012-06-11 26 views
1

我有這個問題,當我加載我的表單時,驗證消息出現在頁面的第一個加載中,我不知道爲什麼.. 這是我的Action 是錯的,如果第一負載動作從A不是一個帖子..I'm混淆來..確認消息出現在表格的第一個加載中

public function inscriptionAction() { 

    $form = new Application_Form_Inscription(); 
    $form->submit->setLabel ('Inscription'); 
    $this->view->form = $form;  

    if ($this->getRequest()->isPost()) { 
     $formData = $this->getRequest()->getPost(); 
     if ($form->isValid ($formData)) { 
      // ton form est valide 
      // => enregistrement des données 
      // => redirection éventuelle 
      $nomU = $form->getValue ('nomU'); 
      $prenomU = $form->getValue ('prenomU'); 
      $mailU = $form->getValue ('mailU'); 
      $dateN = $form->getValue ('dateN'); 
      $civilite = $form->getValue ('civilite'); 
      $villeU = $form->getValue ('villeU'); 
      $passW = $form->getValue ('passW'); 
      $passw2 = $form->getValue ('repassW'); 
      $recevoirNews = (int) $form->getValue ('recevoirNews'); 
      $utilisateurs = new Application_Model_DbTable_Utilisateurs(); 
      $utilisateurs->ajouterUtilisateur ($nomU, $prenomU, $mailU, $passW, $civilite, $dateN, $recevoirNews, $villeU); 

      $this->_helper->redirector ('index'); 

     } else { 
      // ton form est invalide 
      // réinjecte les valeurs saisies par l'user 
      // nouvel affichage du formulaire 
      $form->populate ($formData); 
     } 
    } else { 
     // initialisation et 1er affichage du formulaire 

    } 
} 

感謝的

回答

0

沒有錯,通過GET顯示網頁上的表格的第一負載。

也許在最後一個塊(「// initialisation et 1er affichage du formulaire」)中有某些東西在觸發驗證?

我通常的表單生成/顯示/提交流程沒有明確地調用$form->populate($formData);撥打$form->isValid($formData)對我來說就是這樣。

我流通常是這樣的:

public function myAction() 
{ 
    $form = new My_Form(); 
    $form->setDefaults(array(
     // any defaults I need to set 
    )); 
    if ($this->_request->isPost()){ 
     if ($form->isValid($this->_request->getPost()){ 
      $formData = $form->getValues(); 
      // now write the data 
     } 
    } 
    $this->view->form = $form; 
} 
+0

抱歉來晚了,感謝的對你有所幫助,它的工作原理 – user1448904

+0

高興它幫助。如果是這樣,你可以upvote /接受答案。歡迎來到SO。 ;-) –