Disons que j'ai un blog avec un module « post ».forme Affichage des erreurs de validation dans un modèle (Symfony)
maintenant j'afficher un poste comme celui-ci: après/index id = 1
dans l'action de l'indice i Générez un nouveau CommentForm et passe comme $ this-> forme au modèle et il est affiché en bas d'un post (c'est juste un textfield, rien de spécial). L'action de formulaire est définie sur "post/addcomment". Comment puis-je afficher les erreurs de validation dans ce formulaire? en utilisant setTemplate ('index') ne fonctionne pas parce que je dois passer l'id = 1 ... il
grâce
MISE À JOUR:
est ici un exemple de code:
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());
}
}
}
et mon formulaire de commentaires:
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.'
))
));
}
}
et enfin, 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>
qui est tout.
Je validais la forme comme ils l'ont fait ici: http://www.symfony-project.org/forms/1_4/fr/02-Form-Validation donc je n'utilisais même pas handleError – Jay
return sfView ::SUCCÈS; me prend pour ajouterCommentSuccess et pas indexSuccess ... – Jay
Et vous avez encore l'appel setTemplate là aussi? – sjobe