teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

色々修正しました

2020/05/23 01:49

投稿

gomes_2222
gomes_2222

スコア97

title CHANGED
@@ -1,1 +1,1 @@
1
- cakephpで画像のバリデーション機能の作り方についてアドバイスをださい。
1
+ cakephpで画像のバリデーション機能がうまくいかないです
body CHANGED
@@ -2,28 +2,43 @@
2
2
 
3
3
  cakephpで、いわゆる会員登録画面を作成しています。
4
4
  会員登録画面にアイコン画像を登録するようにします。
5
- その際に、アップロードされたファイルがjpgかpdfかpngのファイル形式なのか判別して、当てはまらない場合はエラー返すようにしたいですが、なかなかうまくいきません
5
+ その際に、アップロードされたファイルがjpgかpdfかpngのファイル形式なのか判別して、当てはまらない場合はエラー返すようにしたいです。
6
6
 
7
- 今のところ、下記のコードでは「Cannot use object of type Laminas\Diactoros\UploadedFile as array」というエラーが出ます。
8
-
9
7
  ### 発生している問題・エラーメッセージ
10
8
 
11
- ```
12
- Cannot use object of type Laminas\Diactoros\UploadedFile as array
9
+ バリデーションがそもそも機能しません。jpgかpdfかpng以外のファイルをアップロードしようとすると、そのまま登録されてしまいます。
13
- ```
14
10
 
15
11
  ### 該当のソースコード
16
12
 
13
+ ```UserTable
17
- ``add.php
14
+ <?php
15
+
16
+ namespace App\Model\Table;
17
+
18
+ use Cake\ORM\Query;
19
+ use Cake\ORM\RulesChecker;
20
+ use Cake\ORM\Table;
21
+ use Cake\Validation\Validator;
22
+
23
+ use Cake\Error\Debugger;
24
+
25
+ class UsersTable extends Table
26
+ {
27
+ public function validationDefault(Validator $validator): Validator
28
+ {$validator
18
- <?= $this->Form->create($user,['type'=>'file'],'multiple') ?>
29
+ ->requirePresence('image', 'create')
19
- <fieldset>
20
- echo $this->Form->control('name');
30
+ ->notEmptyFile('image')
31
+ ->add('image','costom',
32
+ [
33
+ 'rule' => function($value){
21
- echo $this->Form->control('image',['type' => 'file','required' => true,]);
34
+ return preg_match("image/png" || "image/pdf" || "image/jpeg", $value) === 1;
22
- ?>
35
+ },
23
- </fieldset>
24
- <?= $this->Form->button(__('Submit')) ?>
36
+ 'message' => 'ファイルの形式が違います',]);
37
+
25
- <?= $this->Form->end() ?>
38
+ return $validator;
39
+ }
26
40
  ```
41
+
27
42
  ```controller
28
43
  <?php
29
44
  namespace App\Controller;
@@ -42,7 +57,7 @@
42
57
  $user = $this->Users->newEmptyEntity();
43
58
  if($this->request->is('post')) {
44
59
  $file = $this->request->getData('image');
45
- $ext = $file['name']->ext();
60
+ $file_type = $file->getClientMediaType();
46
61
  $filePath = WWW_ROOT. date("YmdHis") . $file->getClientFilename();
47
62
  $data = array(
48
63
  'name' => $this->request->getData('name'),
@@ -61,51 +76,22 @@
61
76
  }
62
77
  ```
63
78
 
64
- ```UserTable
65
- <?php
79
+ ```add.php
66
-
67
- namespace App\Model\Table;
68
-
69
- use Cake\ORM\Query;
70
- use Cake\ORM\RulesChecker;
71
- use Cake\ORM\Table;
72
- use Cake\Validation\Validator;
73
-
74
- use Cake\Error\Debugger;
75
-
76
- class UsersTable extends Table
77
- {
78
- public function validationDefault(Validator $validator): Validator
79
- {$validator
80
- ->requirePresence('image', 'create')
80
+ <?= $this->Form->create($user,['type'=>'file'],'multiple') ?>
81
+ <fieldset>
81
- ->notEmptyFile('image')
82
+ echo $this->Form->control('name');
82
- ->add('image','file_validity',
83
- [
84
- 'rule' => function($value){
85
- return $value == 'application/pdf' || 'application/png' || 'application/jpg';
83
+ echo $this->Form->control('image',['type' => 'file','required' => true,]);
86
- },
84
+ ?>
85
+ </fieldset>
87
- 'message' => 'ファイルの形式が違います',]);
86
+ <?= $this->Form->button(__('Submit')) ?>
88
-
89
- return $validator;
87
+ <?= $this->Form->end() ?>
90
- }
91
88
  ```
92
89
 
93
90
  ### 試したこと
94
91
 
95
- これ以外にカスタムバリデーションファイルを作成し、読み込む方法も試してみてい
92
+ 自分なり色々調べて、カスタムバリデーションで実装する方法をやってみましたダメでした。
96
- そもそもカスタムバリデーションの仕組みを私がよくわからないせいか、使えていない状況です。
97
- 調べてもカスタムバリデーションのコー説明しなものがなく、どこにど情報が渡されるきていないせいか、うまくいってません
93
+ 自分頭では、「UsersTable.php」の$valueにントロラーの$file_typeの値入れ、検証すというのが自分中のすが、うまくいかなようです
98
94
 
99
- ```customvalidation
100
- public static function isImage($value)
101
- {
102
- /*様々な処理*/
103
-
104
- }
105
- ```
106
- 例えば、上記の($value)は、どういった意味で、どこからのデータを読み込む?のでしょうか?
107
95
 
108
-
109
-
110
96
  ### 補足情報(FW/ツールのバージョンなど)
111
97
  cakephp4