回答編集履歴

2

情報の追加

2016/07/01 07:15

投稿

KatsumiTanaka
KatsumiTanaka

スコア924

test CHANGED
@@ -221,3 +221,57 @@
221
221
  </div>
222
222
 
223
223
  ```
224
+
225
+
226
+
227
+ ---
228
+
229
+ 【追記】
230
+
231
+ 画面のHTMLソース
232
+
233
+
234
+
235
+ ```HTML
236
+
237
+ <div class="samples form large-9 medium-8 columns content">
238
+
239
+ <form method="post" accept-charset="utf-8" action="/cakephp30/samples/add">
240
+
241
+ <div style="display:none;">
242
+
243
+ <input type="hidden" name="_method" value="POST"/>
244
+
245
+ </div>
246
+
247
+ <fieldset>
248
+
249
+ <legend>Add Sample</legend>
250
+
251
+ <div class="input text required">
252
+
253
+ <label for="name">Name</label>
254
+
255
+ <input type="text" name="name" required="required" maxlength="45" id="name"/>
256
+
257
+ </div>
258
+
259
+ <div class="input text">
260
+
261
+ <label for="comment">Comment</label>
262
+
263
+ <input type="text" name="comment" maxlength="45" id="comment"/>
264
+
265
+ </div>
266
+
267
+ </fieldset>
268
+
269
+ <button type="submit">Submit</button>
270
+
271
+ </form>
272
+
273
+ </div>
274
+
275
+ ```
276
+
277
+

1

情報の追加(ソースを提示するために、元コメントに追記

2016/07/01 07:14

投稿

KatsumiTanaka
KatsumiTanaka

スコア924

test CHANGED
@@ -15,3 +15,209 @@
15
15
  <?= $this->Form->input('password',['required' => false,'label' => 'パスワード','placeholder' => 'パスワード']); ?>
16
16
 
17
17
  ```
18
+
19
+ ---
20
+
21
+ 【追記2016/07/01】
22
+
23
+ 自分の環境は、テーブルを作成してcontroller,model,templateをbakeしたのち、バリデーションの条件のみを追加していますが、問題なくエラーメッセージが表示されます
24
+
25
+ 自分の環境のコード(といってもほとんどbakeしたままですが)を添付しますので、ご自身のコードとの差分等をご確認ください
26
+
27
+
28
+
29
+
30
+
31
+ ```CakePHP
32
+
33
+ コントローラー
34
+
35
+ /**
36
+
37
+ * Samples Controller
38
+
39
+ *
40
+
41
+ * @property \App\Model\Table\SamplesTable $Samples
42
+
43
+ */
44
+
45
+ class SamplesController extends AppController
46
+
47
+ {
48
+
49
+
50
+
51
+ /**
52
+
53
+ * Add method
54
+
55
+ *
56
+
57
+ * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
58
+
59
+ */
60
+
61
+ public function add()
62
+
63
+ {
64
+
65
+ $sample = $this->Samples->newEntity();
66
+
67
+ if ($this->request->is('post')) {
68
+
69
+ $sample = $this->Samples->patchEntity($sample, $this->request->data);
70
+
71
+ if ($this->Samples->save($sample)) {
72
+
73
+ $this->Flash->success(__('The sample has been saved.'));
74
+
75
+ return $this->redirect(['action' => 'index']);
76
+
77
+ } else {
78
+
79
+ $this->Flash->error(__('The sample could not be saved. Please, try again.'));
80
+
81
+ }
82
+
83
+ }
84
+
85
+ $this->set(compact('sample'));
86
+
87
+ $this->set('_serialize', ['sample']);
88
+
89
+ }
90
+
91
+ }
92
+
93
+
94
+
95
+ モデル
96
+
97
+ /**
98
+
99
+ * Samples Model
100
+
101
+ *
102
+
103
+ */
104
+
105
+ class SamplesTable extends Table
106
+
107
+ {
108
+
109
+
110
+
111
+ /**
112
+
113
+ * Initialize method
114
+
115
+ *
116
+
117
+ * @param array $config The configuration for the Table.
118
+
119
+ * @return void
120
+
121
+ */
122
+
123
+ public function initialize(array $config)
124
+
125
+ {
126
+
127
+ parent::initialize($config);
128
+
129
+
130
+
131
+ $this->table('samples');
132
+
133
+ $this->displayField('name');
134
+
135
+ $this->primaryKey('id');
136
+
137
+
138
+
139
+ $this->addBehavior('Timestamp');
140
+
141
+ }
142
+
143
+
144
+
145
+ /**
146
+
147
+ * Default validation rules.
148
+
149
+ *
150
+
151
+ * @param \Cake\Validation\Validator $validator Validator instance.
152
+
153
+ * @return \Cake\Validation\Validator
154
+
155
+ */
156
+
157
+ public function validationDefault(Validator $validator)
158
+
159
+ {
160
+
161
+ $validator
162
+
163
+ ->integer('id')
164
+
165
+ ->allowEmpty('id', 'create');
166
+
167
+ $validator
168
+
169
+ ->notEmpty('name', '名前を入力してください')
170
+
171
+ ->notEmpty('comment','コメントを入力してください')
172
+
173
+ ->add('comment',['length'=> ['rule' => ['minLength', 8],'message' => 'コメントは8文字以上で設定してください']]);
174
+
175
+
176
+
177
+ return $validator;
178
+
179
+ }
180
+
181
+ }
182
+
183
+
184
+
185
+ テンプレート(addのみ)
186
+
187
+ <nav class="large-3 medium-4 columns" id="actions-sidebar">
188
+
189
+ <ul class="side-nav">
190
+
191
+ <li class="heading"><?= __('Actions') ?></li>
192
+
193
+ <li><?= $this->Html->link(__('List Samples'), ['action' => 'index']) ?></li>
194
+
195
+ </ul>
196
+
197
+ </nav>
198
+
199
+ <div class="samples form large-9 medium-8 columns content">
200
+
201
+ <?= $this->Form->create($sample) ?>
202
+
203
+ <fieldset>
204
+
205
+ <legend><?= __('Add Sample') ?></legend>
206
+
207
+ <?php
208
+
209
+ echo $this->Form->input('name',['required' => false, 'label' => '名前','placeholder' => '名前']);
210
+
211
+ echo $this->Form->input('comment',['required' => false, 'label' => 'コメント', 'placeholder' => 'コメント']);
212
+
213
+ ?>
214
+
215
+ </fieldset>
216
+
217
+ <?= $this->Form->button(__('Submit')) ?>
218
+
219
+ <?= $this->Form->end() ?>
220
+
221
+ </div>
222
+
223
+ ```