假設我有一個模塊「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