【问题标题】:Displaying form validation errors in a template (Symfony)在模板中显示表单验证错误 (Symfony)
【发布时间】:2010-04-07 13:29:54
【问题描述】:

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

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

在索引操作中,我生成了一个新的 CommentForm 并将其作为 $this->form 传递给模板,它显示在帖子的底部(它只是一个文本字段,没什么特别的)。表单操作设置为“发布/添加评论”。如何在此表单中显示验证错误?使用 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>

就是这样。

【问题讨论】:

    标签: validation forms symfony1


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您是否在操作中使用了 handleError 方法?如果在 handleError 方法中执行 return sfView::SUCCESS;

      ,则 url 的 id=1 部分不应更改

      更新:

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

      【讨论】:

      • 我正在验证表单,就像他们在这里所做的那样:symfony-project.org/forms/1_4/en/02-Form-Validation 所以我什至没有使用 handleError
      • 你还有 setTemplate 调用吗?
      • 也可以使用其中的 setTemplate 进行尝试。我使用的是 symfony 1.0,而不是 1.4,但我认为情况并没有太大变化。
      【解决方案3】:

      尝试将表单操作更改为

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-12
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-26
        相关资源
        最近更新 更多