Validation 在模板中显示表单验证错误(Symfony)

Validation 在模板中显示表单验证错误(Symfony),validation,forms,symfony1,Validation,Forms,Symfony1,比如说,我有一个带有模块帖子的博客 现在我显示这样一个帖子:post/index?id=1 在索引操作中,我生成了一个新的CommentForm,并将其作为$this->form传递给模板,它显示在帖子的底部,只是一个文本字段,没有什么特别的。表单操作设置为post/addcomment。如何在此表单中显示验证错误?使用setTemplate'index'不起作用,因为我必须将id=1传递给它 谢谢 更新: 下面是一个示例代码: public function executeIndex(sf

比如说,我有一个带有模块帖子的博客

现在我显示这样一个帖子:post/index?id=1

在索引操作中,我生成了一个新的CommentForm,并将其作为$this->form传递给模板,它显示在帖子的底部,只是一个文本字段,没有什么特别的。表单操作设置为post/addcomment。如何在此表单中显示验证错误?使用setTemplate'index'不起作用,因为我必须将id=1传递给它

谢谢

更新:

下面是一个示例代码:

  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>

就是这样。

您是否在操作中使用handleError方法?如果在handleError方法中执行返回sfView::SUCCESS,则url的id=1部分不应更改

更新:


它实际上会发生变化,您需要做的是将id与注释一起提交[我确信您已经这样做了,因为不引用帖子的注释没有多大意义],然后在handleError方法中,在那里实例化post对象

如果您使用的是sf 1.4,只需将executeAddComments和executeIndex放在一个函数executeIndex中即可。setTemplate在此不起作用。

尝试将表单更改为操作

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

这样,即使在post请求中,post id参数也应该可用,它应该与setTemplate'index'或executeAddComment末尾的forward一起使用

我在验证表单,就像他们在这里所做的那样:所以我甚至没有使用HandleError,而且那里还有setTemplate调用?也可以使用setTemplate尝试一下。我使用symfony 1.0,而不是1.4,但我认为事情并没有发生太大的变化。