假设我有一个模块“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>
就是这样。
我有效的形式像他们这样做:http://www.symfony-project.org/forms/1_4/en/02-Form-Validation所以我甚至没有使用handleError – Jay 2010-04-07 13:58:48
返回sfView ::成功;需要我添加评论成功,而不是indexSuccess ... – Jay 2010-04-07 14:26:42
你还有setTemplate调用吗? – sjobe 2010-04-07 15:29:40