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

回答編集履歴

2

情報の追加

2016/07/01 07:15

投稿

KatsumiTanaka
KatsumiTanaka

スコア924

answer CHANGED
@@ -109,4 +109,30 @@
109
109
  <?= $this->Form->button(__('Submit')) ?>
110
110
  <?= $this->Form->end() ?>
111
111
  </div>
112
- ```
112
+ ```
113
+
114
+ ---
115
+ 【追記】
116
+ 画面のHTMLソース
117
+
118
+ ```HTML
119
+ <div class="samples form large-9 medium-8 columns content">
120
+ <form method="post" accept-charset="utf-8" action="/cakephp30/samples/add">
121
+ <div style="display:none;">
122
+ <input type="hidden" name="_method" value="POST"/>
123
+ </div>
124
+ <fieldset>
125
+ <legend>Add Sample</legend>
126
+ <div class="input text required">
127
+ <label for="name">Name</label>
128
+ <input type="text" name="name" required="required" maxlength="45" id="name"/>
129
+ </div>
130
+ <div class="input text">
131
+ <label for="comment">Comment</label>
132
+ <input type="text" name="comment" maxlength="45" id="comment"/>
133
+ </div>
134
+ </fieldset>
135
+ <button type="submit">Submit</button>
136
+ </form>
137
+ </div>
138
+ ```

1

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

2016/07/01 07:14

投稿

KatsumiTanaka
KatsumiTanaka

スコア924

answer CHANGED
@@ -6,4 +6,107 @@
6
6
 
7
7
  ```CakePHP
8
8
  <?= $this->Form->input('password',['required' => false,'label' => 'パスワード','placeholder' => 'パスワード']); ?>
9
+ ```
10
+ ---
11
+ 【追記2016/07/01】
12
+ 自分の環境は、テーブルを作成してcontroller,model,templateをbakeしたのち、バリデーションの条件のみを追加していますが、問題なくエラーメッセージが表示されます
13
+ 自分の環境のコード(といってもほとんどbakeしたままですが)を添付しますので、ご自身のコードとの差分等をご確認ください
14
+
15
+
16
+ ```CakePHP
17
+ コントローラー
18
+ /**
19
+ * Samples Controller
20
+ *
21
+ * @property \App\Model\Table\SamplesTable $Samples
22
+ */
23
+ class SamplesController extends AppController
24
+ {
25
+
26
+ /**
27
+ * Add method
28
+ *
29
+ * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
30
+ */
31
+ public function add()
32
+ {
33
+ $sample = $this->Samples->newEntity();
34
+ if ($this->request->is('post')) {
35
+ $sample = $this->Samples->patchEntity($sample, $this->request->data);
36
+ if ($this->Samples->save($sample)) {
37
+ $this->Flash->success(__('The sample has been saved.'));
38
+ return $this->redirect(['action' => 'index']);
39
+ } else {
40
+ $this->Flash->error(__('The sample could not be saved. Please, try again.'));
41
+ }
42
+ }
43
+ $this->set(compact('sample'));
44
+ $this->set('_serialize', ['sample']);
45
+ }
46
+ }
47
+
48
+ モデル
49
+ /**
50
+ * Samples Model
51
+ *
52
+ */
53
+ class SamplesTable extends Table
54
+ {
55
+
56
+ /**
57
+ * Initialize method
58
+ *
59
+ * @param array $config The configuration for the Table.
60
+ * @return void
61
+ */
62
+ public function initialize(array $config)
63
+ {
64
+ parent::initialize($config);
65
+
66
+ $this->table('samples');
67
+ $this->displayField('name');
68
+ $this->primaryKey('id');
69
+
70
+ $this->addBehavior('Timestamp');
71
+ }
72
+
73
+ /**
74
+ * Default validation rules.
75
+ *
76
+ * @param \Cake\Validation\Validator $validator Validator instance.
77
+ * @return \Cake\Validation\Validator
78
+ */
79
+ public function validationDefault(Validator $validator)
80
+ {
81
+ $validator
82
+ ->integer('id')
83
+ ->allowEmpty('id', 'create');
84
+ $validator
85
+ ->notEmpty('name', '名前を入力してください')
86
+ ->notEmpty('comment','コメントを入力してください')
87
+ ->add('comment',['length'=> ['rule' => ['minLength', 8],'message' => 'コメントは8文字以上で設定してください']]);
88
+
89
+ return $validator;
90
+ }
91
+ }
92
+
93
+ テンプレート(addのみ)
94
+ <nav class="large-3 medium-4 columns" id="actions-sidebar">
95
+ <ul class="side-nav">
96
+ <li class="heading"><?= __('Actions') ?></li>
97
+ <li><?= $this->Html->link(__('List Samples'), ['action' => 'index']) ?></li>
98
+ </ul>
99
+ </nav>
100
+ <div class="samples form large-9 medium-8 columns content">
101
+ <?= $this->Form->create($sample) ?>
102
+ <fieldset>
103
+ <legend><?= __('Add Sample') ?></legend>
104
+ <?php
105
+ echo $this->Form->input('name',['required' => false, 'label' => '名前','placeholder' => '名前']);
106
+ echo $this->Form->input('comment',['required' => false, 'label' => 'コメント', 'placeholder' => 'コメント']);
107
+ ?>
108
+ </fieldset>
109
+ <?= $this->Form->button(__('Submit')) ?>
110
+ <?= $this->Form->end() ?>
111
+ </div>
9
112
  ```