0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CakePHP3 リレーション関係にあるテーブルに対してバリデーションを掛けたい

Posted at

前提

  • PHP
    • 7.3.11
  • CakePHP
    • v3.6.14
  • MySQL
    • 5.7.26

0. テーブル構造

mysql> desc users;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | int(11)      | NO   | PRI | NULL    | auto_increment |
| name          | varchar(255) | NO   |     | NULL    |                |
| tel           | varchar(255) | NO   |     | NULL    |                |
| email         | varchar(255) | NO   | MUL | NULL    |                |
| created       | datetime     | NO   |     | NULL    |                |
| modified      | datetime     | NO   |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
19 rows in set (0.00 sec)

mysql> desc addresses;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id       | int(11)      | NO   |     | NULL    |                |
| city          | varchar(255) | NO   |     | NULL    |                |
| building      | varchar(255) | NO   | MUL | NULL    |                |
| created       | datetime     | NO   |     | NULL    |                |
| modified      | datetime     | NO   |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
19 rows in set (0.00 sec)

1. 保存したいデータ

  • リレーション関係のような階層構造を持った連想配列にする
$data = [
	'user' => [
		'email' => 'hoge@example.com',
		'tel' => '09012345678',
		'name' => '山田太郎',
		'address' => [
			'city' => '2200011',
			'building' => '14',
			'address' => '横浜市西区高島',
			'building' => '123'
		],
	],
];

2. バリデーション

  • リレーション関係のような階層構造を持った連想配列にする
$validate = [
    'associated' => [
        'Addresses' => [
            'validate' => 'default',
        ],
    ],
];

バリデーションクラス

/**
 * Default validation rules.
 *
 * @param \Cake\Validation\Validator $validator Validator instance.
 * @return \Cake\Validation\Validator
 */
public function validationDefault(Validator $validator)
{
    $validator->notEmpty('city');
    $validator->notEmpty('building');

    return $validator;
}

3. オブジェクト生成

  • 第一引数に保存したいデータを、第二引数にバリデーションを渡す
$user = $this->Users->newEntity($data, $validate);

4. エラー検知

  • バリデーションエラーに引っかかってる場合、以下のメソッドでエラー内容を取得することが可能
    • EntityTrait::getError() or EntityTrait::getErrors()
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?