2

So, validation errors show up in my add action, but not in my edit action. Here are the snippets from my controller:

Here I get validation error messages as expected:

    public function add() {
        if ($this->request->is('post')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved.'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please try again.'));
            }
        }
        $this->set('clients', $this->User->Client->find('list'));
    }

But not here:

    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved.'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
        $this->set('user', $this->User->read());
        $this->set('clients', $this->User->Client->find('list'));
    }
2
  • Typo on this line: if ($this->User->save($this->request->data/)) - extra / at the end Commented Apr 16, 2012 at 13:18
  • Ah, there was a comment in my original code that I removed on SO. Commented Apr 16, 2012 at 13:21

2 Answers 2

3

If i recall correctly, using the read() call after a failed save will clear the validation errors.

there.. i found it http://book.cakephp.org/1.3/view/1017/Retrieving-Your-Data#read-1029

Sign up to request clarification or add additional context in comments.

1 Comment

great find @yossi i was going nuts on this issue.
0

Is the setFlash('user could not be saved') message firing?

What's the ouput of debug($this->User->validationErrors) - add it after the setFlash fail message

Does your post array contain all the fields it needs to save? Do you have a required field in your $validate that you don't have in your post array? (Cake will register the error, but won't be able to display it unless you have the field in your form). Or another validate rule that is failing but you aren't displaying the field in your form?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.