LoginSignup
8
7

More than 5 years have passed since last update.

CakePHPのValidationのエラー情報を取得

Posted at

いつも忘れるのでメモ

Validationのエラーは$model->validationErrorsに入っている
http://api.cakephp.org/2.4/class-Model.html#$validationErrors

<?php

class UsersController extends AppController {
    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $msg = __('The user has been saved.' );
                $this->Session->setFlash($msg);
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user data could not be saved. Please, try again.'));

                // validationエラーをログに残す
                $this->log("validationErrors=" . var_export($this->User->validationErrors, true));
            }
        }
    }
}

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7