2010-04-07 64 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

相关问题