I have thread show page (thread/show) with reply form on the bottom of the page. Replay form posts to post/add action. When reply is successfully added i can redirect everywhere. The problem is when there are some errors for example reply form is empty and post/add is called. I need to go to thread/show instead (with 'errors' array) and show errors there, because i have reply form there.

try {$forumPost->save();
            } catch (ORM_Validation_Exception $e) {
                $errors = $e->errors('');
               //I need change url here to thread/show
            }

Is it possible?

有帮助吗?

解决方案

You can store the errors in the Session array. So they'll persist across multiple requests

try {
     $forumPost->save();
} catch (ORM_Validation_Exception $e) {
     $errors = $e->errors('');

     Session::instance()->set('thread_add_errors', $errors);
     HTTP::redirect('thread/show');
}

Then in the thread show view

<?php if($errors = Session::instance()->get('thread_add_errors')): ?>
      // Show errors

      // Don't forget to delete the error :)
      <?php Session::instance()->delete('thread_add_errors'); ?>
<?php endif; ?>

其他提示

Long since I haven't used Kohana but,

 $this->template->content = View::factory('thread/show')
        ->bind('errors', $errors); 

This should be like this. You are setting variable to your view, variable can of course be an array of errors like yours.

You can also bind multiple variables to the view this way

$this->template->content = View::factory('thread/show')
    ->bind('user', $user)
    ->bind('message', $message)
    ->bind('errors', $errors); 

I hope this is what you actually wanted!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top