views:

544

answers:

3

Hi,

let's say I have a blog with a module "post".

now I display a post like this: post/index?id=1

in the index-action i generate a new CommentForm and pass it as $this->form to the template and it is being displayed at the bottom of a post (it's just a textfield, nothing special). form action is set to "post/addcomment". How can I display the validation errors in this form? using setTemplate('index') doesn't work because I would have to pass the id=1 to it...

thanks

UPDATE:

here's a sample 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());
      }
  }

}

and my Comment Form:

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.'
                            ))
        ));
    }
}

and finally, 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>

that's it.

A: 

Are you using the handleError method in the action ? The id=1 part of your url should not change if inside the handleError method, you do a return sfView::SUCCESS;

UPDATE:

It actually changes, what you need to do is submit the id along with the comment [Which I'm sure you're already doing because a comment that doesn't refer to a post doesn't make much sense], then in your handleError method, instantiate the post object there.

sjobe
I was validatig the form like they did it here: http://www.symfony-project.org/forms/1_4/en/02-Form-Validation so I wasn't even using handleError
Jay
return sfView::SUCCESS; takes me to addCommentSuccess and not indexSuccess...
Jay
And you still have the setTemplate call in there too ?
sjobe
no I don't, i've already removed it
Jay
Try it with the setTemplate in there too. I use symfony 1.0, not 1.4 but I don't think things have changed that much.
sjobe
A: 

If you're using sf 1.4 just put executeAddComments and executeIndex together in one function (executeIndex for example) and you'll be fine. setTemplate won't work here.

Cav
A: 

Try to change your form action to

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

Doing this, your post id parameter should be available even on your post request, and it should work with setTemplate('index') or forward at the end of executeAddComment

Benoit