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

时间:2010-04-07 13:29:54

标签: validation forms symfony1

假设我有一个带有“post”模块的博客。

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

在index-action中我生成一个新的CommentForm并将其作为$ this->表单传递给模板,它显示在帖子的底部(它只是一个文本字段,没什么特别的)。表单操作设置为“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>

就是这样。

3 个答案:

答案 0 :(得分:0)

您是否在操作中使用handleError方法?如果在handleError方法中,你返回sfView :: SUCCESS;

,你的url的id = 1部分不应该改变

更新:

它实际上发生了变化,你需要做的是提交id和评论[我确定你已经在做了,因为没有引用帖子的评论没有多大意义],然后在你的handleError方法中,实例化post对象。

答案 1 :(得分:0)

如果您正在使用sf 1.4,只需将executeAddComments和executeIndex放在一个函数中(例如executeIndex),您就可以了。 setTemplate在这里不起作用。

答案 2 :(得分:0)

尝试将表单操作更改为

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

这样做,你的post id参数应该在你的帖子请求中可用,它应该与setTemplate('index')一起使用或者在executeAddComment结束时转发

相关问题