質問編集履歴

1

色々修正しました

2020/05/23 01:49

投稿

gomes_2222
gomes_2222

スコア90

test CHANGED
@@ -1 +1 @@
1
- cakephpで画像のバリデーション機能の作り方についてアドバイスをださい。
1
+ cakephpで画像のバリデーション機能がうまくいかないです
test CHANGED
@@ -6,11 +6,7 @@
6
6
 
7
7
  会員登録画面にアイコン画像を登録するようにします。
8
8
 
9
- その際に、アップロードされたファイルがjpgかpdfかpngのファイル形式なのか判別して、当てはまらない場合はエラー返すようにしたいですが、なかなかうまくいきません
9
+ その際に、アップロードされたファイルがjpgかpdfかpngのファイル形式なのか判別して、当てはまらない場合はエラー返すようにしたいです。
10
-
11
-
12
-
13
- 今のところ、下記のコードでは「Cannot use object of type Laminas\Diactoros\UploadedFile as array」というエラーが出ます。
14
10
 
15
11
 
16
12
 
@@ -18,109 +14,11 @@
18
14
 
19
15
 
20
16
 
21
- ```
22
-
23
- Cannot use object of type Laminas\Diactoros\UploadedFile as array
17
+ バリデーションがそもそも機能しません。jpgかpdfpng以外のファイルをアップロードしようとすると、そのまま登録されてしまいます。
24
-
25
- ```
26
18
 
27
19
 
28
20
 
29
21
  ### 該当のソースコード
30
-
31
-
32
-
33
- ``add.php
34
-
35
- <?= $this->Form->create($user,['type'=>'file'],'multiple') ?>
36
-
37
- <fieldset>
38
-
39
- echo $this->Form->control('name');
40
-
41
- echo $this->Form->control('image',['type' => 'file','required' => true,]);
42
-
43
- ?>
44
-
45
- </fieldset>
46
-
47
- <?= $this->Form->button(__('Submit')) ?>
48
-
49
- <?= $this->Form->end() ?>
50
-
51
- ```
52
-
53
- ```controller
54
-
55
- <?php
56
-
57
- namespace App\Controller;
58
-
59
-
60
-
61
- use App\Controller\AppController;
62
-
63
- use Cake\Event\EventInterface;
64
-
65
- use Exception;
66
-
67
- use Cake\Filesystem\Folder;
68
-
69
- use Cake\Filesystem\File;
70
-
71
- use RuntimeException;
72
-
73
-
74
-
75
- class UserController extends AppController
76
-
77
- {
78
-
79
- public function add()
80
-
81
- {
82
-
83
- $user = $this->Users->newEmptyEntity();
84
-
85
- if($this->request->is('post')) {
86
-
87
- $file = $this->request->getData('image');
88
-
89
- $ext = $file['name']->ext();
90
-
91
- $filePath = WWW_ROOT. date("YmdHis") . $file->getClientFilename();
92
-
93
- $data = array(
94
-
95
- 'name' => $this->request->getData('name'),
96
-
97
- 'image' => date("YmdHis") . $file ->getClientFilename()
98
-
99
- );
100
-
101
-
102
-
103
- $user = $this->Users->patchEntity($biditem,$data);
104
-
105
- if($this->Users->save($user)) {
106
-
107
- $file->moveTo($filePath);
108
-
109
- $this->Flash->success(__('保存しました。'));
110
-
111
- return $this->redirect(['action' =>'index']);
112
-
113
- }
114
-
115
- $this->Flash->error(__('保存に失敗しました。もう一度入力下さい。'));
116
-
117
- }
118
-
119
- $this->set(compact('user'));
120
-
121
- }
122
-
123
- ```
124
22
 
125
23
 
126
24
 
@@ -160,13 +58,13 @@
160
58
 
161
59
  ->notEmptyFile('image')
162
60
 
163
- ->add('image','file_validity',
61
+ ->add('image','costom',
164
62
 
165
63
  [
166
64
 
167
65
  'rule' => function($value){
168
66
 
169
- return $value == 'application/pdf' || 'application/png' || 'application/jpg';
67
+ return preg_match("image/png" || "image/pdf" || "image/jpeg", $value) === 1;
170
68
 
171
69
  },
172
70
 
@@ -182,35 +80,109 @@
182
80
 
183
81
 
184
82
 
83
+ ```controller
84
+
85
+ <?php
86
+
87
+ namespace App\Controller;
88
+
89
+
90
+
91
+ use App\Controller\AppController;
92
+
93
+ use Cake\Event\EventInterface;
94
+
95
+ use Exception;
96
+
97
+ use Cake\Filesystem\Folder;
98
+
99
+ use Cake\Filesystem\File;
100
+
101
+ use RuntimeException;
102
+
103
+
104
+
105
+ class UserController extends AppController
106
+
107
+ {
108
+
109
+ public function add()
110
+
111
+ {
112
+
113
+ $user = $this->Users->newEmptyEntity();
114
+
115
+ if($this->request->is('post')) {
116
+
117
+ $file = $this->request->getData('image');
118
+
119
+ $file_type = $file->getClientMediaType();
120
+
121
+ $filePath = WWW_ROOT. date("YmdHis") . $file->getClientFilename();
122
+
123
+ $data = array(
124
+
125
+ 'name' => $this->request->getData('name'),
126
+
127
+ 'image' => date("YmdHis") . $file ->getClientFilename()
128
+
129
+ );
130
+
131
+
132
+
133
+ $user = $this->Users->patchEntity($biditem,$data);
134
+
135
+ if($this->Users->save($user)) {
136
+
137
+ $file->moveTo($filePath);
138
+
139
+ $this->Flash->success(__('保存しました。'));
140
+
141
+ return $this->redirect(['action' =>'index']);
142
+
143
+ }
144
+
145
+ $this->Flash->error(__('保存に失敗しました。もう一度入力下さい。'));
146
+
147
+ }
148
+
149
+ $this->set(compact('user'));
150
+
151
+ }
152
+
153
+ ```
154
+
155
+
156
+
157
+ ```add.php
158
+
159
+ <?= $this->Form->create($user,['type'=>'file'],'multiple') ?>
160
+
161
+ <fieldset>
162
+
163
+ echo $this->Form->control('name');
164
+
165
+ echo $this->Form->control('image',['type' => 'file','required' => true,]);
166
+
167
+ ?>
168
+
169
+ </fieldset>
170
+
171
+ <?= $this->Form->button(__('Submit')) ?>
172
+
173
+ <?= $this->Form->end() ?>
174
+
175
+ ```
176
+
177
+
178
+
185
179
  ### 試したこと
186
180
 
187
181
 
188
182
 
189
- これ以外にカスタムバリデーションファイルを作成し、読み込む方法も試してみてい
183
+ 自分なり色々調べて、カスタムバリデーションで実装する方法をやってみましたダメでした。
190
184
 
191
- そもそもカスタムバリデーションの仕組みを私がよくわからないせいか、使えていない状況です。
192
-
193
- 調べてもカスタムバリデーションのコー説明しているよなものがなく、どこにど情報が渡されるきていないせいか、うまくいってません
185
+ 自分頭では、「UsersTable.php」の$valueにントロラーの$file_typeの値入れ、検証するというのが自分中のすが、うまくいかなようです
194
-
195
-
196
-
197
- ```customvalidation
198
-
199
- public static function isImage($value)
200
-
201
- {
202
-
203
- /*様々な処理*/
204
-
205
-
206
-
207
- }
208
-
209
- ```
210
-
211
- 例えば、上記の($value)は、どういった意味で、どこからのデータを読み込む?のでしょうか?
212
-
213
-
214
186
 
215
187
 
216
188