0

I am trying a Zend framework 3 tutorial and am getting stuck in "editing" a function in the in-depth part (Blog case).

When trying to edit a blog message, the editing form doesn't show the original message. It seems that the original message couldn't be bound to the form.

I copied all the sample code. I don't know what is wrong with it. By the way, my add and delete function work fine.

can anyone help me with it?

The editAction method from the tutorial:

public function editAction()
{
    $id = $this->params()->fromRoute('id');
    if (! $id) {
        return $this->redirect()->toRoute('blog');
    }
    try {
        $post = $this->repository->findPost($id);
    } catch (InvalidArgumentException $ex) {
        return $this->redirect()->toRoute('blog');
    }
    $this->form->bind($post);
    $viewModel = new ViewModel(['form' => $this->form]);
    $request = $this->getRequest();
    if (! $request->isPost()) {
        return $viewModel;
    }
    $this->form->setData($request->getPost());
    if (! $this->form->isValid()) {
        return $viewModel;
    }
    $post = $this->command->updatePost($post);
    return $this->redirect()->toRoute(
        'blog/detail',
        ['id' => $post->getId()]
    );
}
Wilt
  • 41,477
  • 12
  • 152
  • 203
Stwo
  • 1
  • 1
  • 2
  • Can you edit your question to include the code for your `editAction` function in the controller? – Tim Fountain Aug 13 '16 at 16:14
  • Hi Tim, thanks for your reply. – Stwo Aug 14 '16 at 22:35
  • I fixed my problem with correct init() function in PostForm.phtml. Thanks. – Stwo Aug 15 '16 at 04:35
  • Stwo, sounds like you solved it. Maybe you can answer your own question and accept the answer so that the question gets closed... The answer could be interesting for others. Also consider changing the title to something more descriptive so people with similar problems will find your post... – Wilt Aug 22 '16 at 07:28
  • It is not really an tutorial issue. but it is a bit fuzzy in PostForm page when modifying init() function. – Stwo Sep 04 '16 at 23:41

1 Answers1

0

Edit this code:

if (! $request->isPost()) {
    foreach($this->form->getMessages() as $message){
        $this->flashMessenger()->addErrorMessage($message['message']);
    }
}

In your view:

<?php echo $this->flashMessenger()->renderCurrent('error', ['options go here...']); ?>
CASH
  • 93
  • 5