2010-04-07 78 views
0

假設我有一個模塊「post」的博客。在模板中顯示錶單驗證錯誤(Symfony)

現在我顯示一個柱是這樣的:交/索引在索引動作ID = 1

我生成新CommentForm並將其傳遞通過$ this->形式模板和正在顯示它在帖子的底部(這只是一個文本框,沒有什麼特別的)。表單操作設置爲「發佈/添加評論」。我如何在此表單中顯示驗證錯誤?使用setTemplate( '指數')不起作用,因爲我將不得不ID傳遞= 1來......

感謝

UPDATE:

這裏是一個示例代碼:

public function executeIndex(sfWebRequest $request) 
    { 
     $post = Doctrine::getTable('Posts')->find($request->getParameter('id')); 
     $this->post = $post->getContent(); 

     $comments = $post->getComment(); 

     if ($comments->count() > 0) 
       $this->comments = $comments; 

     $this->form = new CommentForm(); 
     $this->form->setDefault('pid', $post->getPrimaryKey()); 
    } 

    public function executeAddComment(sfWebRequest $request) { 
    $this->form = new CommentForm(); 

    if ($request->isMethod('post') && $request->hasParameter('comment')) { 
     $this->form->bind($request->getParameter('comment')); 
     if ($this->form->isValid()) { 
      $comment = new Comment(); 
      $comment->setPostId($this->form->getValue('pid')); 
      $comment->setComment($this->form->getValue('comment')); 
      $comment->save(); 
      $this->redirect('show/index?id='.$comment->getPostId()); 
     } 
    } 

}

和我的意見表:

class CommentForm extends BaseForm { 
    public function configure() { 
     $this->setWidgets(array(
       'comment'  => new sfWidgetFormTextarea(), 
       'pid'   => new sfWidgetFormInputHidden() 
     )); 

     $this->widgetSchema->setNameFormat('comment[%s]'); 

     $this->setValidators(array(
       'comment' => new sfValidatorString(
         array(
          'required' => true, 
          'min_length' => 5 
          ), 
         array(
          'required' => 'The comment field is required.', 
          'min_length' => 'The message "%value%" is too short. It must be of %min_length% characters at least.' 
          )), 
       'pid'  => new sfValidatorNumber(
         array(
          'required' => true, 
          'min'  => 1, 
          'max'  => 4294967295 
          ), 
         array(
          'required' => 'Some fields are missing.' 
          )) 
     )); 
    } 
} 

最後,indexSuccess:

<?php echo $post; ?> 

//show comments (skipped) 

<h3>Add a comment</h3> 

<form action="<?php echo url_for('show/addComment') ?>" method="POST"> 
    <table> 
     <?php echo $form ?> 
     <tr> 
      <td colspan="2"> 
       <input type="submit" /> 
      </td> 
     </tr> 
    </table> 
</form> 

就是這樣。

回答

0

如果您使用sf 1.4,只需將一個函數中的executeAddComments和executeIndex放在一起(例如executeIndex)就可以了。 setTemplate在這裏不起作用。

0

您是否在動作中使用了handleError方法?如果在handleError方法內,你的url的id = 1部分不應該改變,你可以返回sfView :: SUCCESS;

UPDATE:

它實際上改變了,你需要做的是與評論[我確信你已經做一起提交ID,因爲這並不指後沒有按評論'沒什麼意義],然後在你的handleError方法中,在那裏實例化post對象。

+0

我有效的形式像他們這樣做:http://www.symfony-project.org/forms/1_4/en/02-Form-Validation所以我甚至沒有使用handleError – Jay 2010-04-07 13:58:48

+0

返回sfView ::成功;需要我添加評論成功,而不是indexSuccess ... – Jay 2010-04-07 14:26:42

+0

你還有setTemplate調用嗎? – sjobe 2010-04-07 15:29:40

0

試圖改變自己的形式作用於

<?php echo url_for('show/addComment?id=' . $post->getId()) ?> 

這樣做,你的帖子ID參數應可連上你的POST請求,並應與setTemplate(「索引」)工作,或在年底轉發的executeAddComment

相關問題