質問編集履歴
1
ソースコード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,229 @@
|
|
1
1
|
タイトルの通りですが、一つのFormでSubmitした際、まったく別のFormでもバリデーションが反応してしまって困っています。例えば記事投稿ページのPost.titleというフィールドと、全ページ共通部分のタイトル検索フィールドPost.titleなどは同一ページ上に存在します。
|
2
2
|
|
3
3
|
これを回避する手段はあるでしょうか?お手数ですがご教授頂ければ幸いです。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
<追記>
|
8
|
+
|
9
|
+
ありがとうございます。
|
10
|
+
|
11
|
+
addページでtitleがnullの状態で、Submitするとaddページ、検索フォームともに”このフィールドは入力必須です”のメッセージが出力されてしまう状態です。
|
12
|
+
|
13
|
+
actionが異なっているのにバリデーションが反応してしまうこと自体がおかしいのでしょうか?
|
14
|
+
|
15
|
+
searchはsearch pluginを使用しています。
|
16
|
+
|
17
|
+
お二人に教えていただいた内容も試してみましたが、うまく設定できていないのか機能してくれません。。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
posts/add (新規記事投稿)
|
22
|
+
|
23
|
+
posts/search (記事タイトル検索)
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
Post.php <Model>
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
public $actsAs = array('Search.Searchable');
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
public $validate = array(
|
36
|
+
|
37
|
+
'title' => array(
|
38
|
+
|
39
|
+
'rule' => 'notBlank',
|
40
|
+
|
41
|
+
'message' => 'このフィールドは入力必須です。'
|
42
|
+
|
43
|
+
)
|
44
|
+
|
45
|
+
);
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
public $filterArgs = array(
|
50
|
+
|
51
|
+
array('name' => 'title', 'type' => 'like')
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
PostsController.php
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
public function add() {
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
$this->loadModel('Category', 'Tag');
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
// add to categories table
|
72
|
+
|
73
|
+
$this->set('posts', $this->Category->find('list', array('fields' => 'Category.name')));
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
// tag
|
78
|
+
|
79
|
+
$this->set('tag', $this->Tag->find('list'));
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
if ($this->request->is('post')) {
|
84
|
+
|
85
|
+
$this->request->data['Post']['user_id'] = $this->Auth->user('id');
|
86
|
+
|
87
|
+
#$this->Post->create();
|
88
|
+
|
89
|
+
if ($this->Post->saveAll($this->request->data)) {
|
90
|
+
|
91
|
+
$this->Session->setFlash(__('Your post has been saved.'), 'alert', array(
|
92
|
+
|
93
|
+
'plugin' => 'BoostCake',
|
94
|
+
|
95
|
+
'class' => 'alert-success'
|
96
|
+
|
97
|
+
));
|
98
|
+
|
99
|
+
return $this->redirect(array('action' => 'index'));
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
// tagが2つ以上選択されていない場合のエラーメッセージを取得
|
106
|
+
|
107
|
+
$errors = $this->Post->validationErrors;
|
108
|
+
|
109
|
+
if (!empty($errors)) $this->set('tag_error', $errors['Tag']);
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
$this->Session->setFlash(__('Unable to add your post.'), 'alert', array(
|
114
|
+
|
115
|
+
'plugin' => 'BoostCake',
|
116
|
+
|
117
|
+
'class' => 'alert-danger'
|
118
|
+
|
119
|
+
));
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
public function search() {
|
128
|
+
|
129
|
+
#$this->Post->recursive = 2;
|
130
|
+
|
131
|
+
$this->Prg->commonProcess();
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
$conditions = $this->Post->parseCriteria($this->Prg->parsedParams());
|
136
|
+
|
137
|
+
if (!empty($conditions)) {
|
138
|
+
|
139
|
+
$this->set('posts', $this->paginate(array(
|
140
|
+
|
141
|
+
$conditions
|
142
|
+
|
143
|
+
))
|
144
|
+
|
145
|
+
);
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
```
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
sidebar.ctp(search)
|
156
|
+
|
157
|
+
```
|
158
|
+
|
159
|
+
<?php
|
160
|
+
|
161
|
+
echo $this->Form->create('Post', array(
|
162
|
+
|
163
|
+
'novalidate' => true,
|
164
|
+
|
165
|
+
'url' => array_merge(array('action' => 'search'), $this->params['pass'])
|
166
|
+
|
167
|
+
));
|
168
|
+
|
169
|
+
echo $this->Form->input('Post.title', array('div' => 'form-group', 'class' => 'form-control'));
|
170
|
+
|
171
|
+
echo $this->Form->end();
|
172
|
+
|
173
|
+
?>
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
add.ctp(create)
|
180
|
+
|
181
|
+
```
|
182
|
+
|
183
|
+
<?php
|
184
|
+
|
185
|
+
echo $this->Form->create('Post', array('type' => 'file', 'novalidate' => true));
|
186
|
+
|
187
|
+
echo $this->Form->input('title', array('class' => 'form-control', 'div' => 'form-group'));
|
188
|
+
|
189
|
+
echo $this->Form->input('body', array('rows' => '3', 'class' => 'form-control', 'div' => 'form-group'));
|
190
|
+
|
191
|
+
echo $this->Form->input('category_id', array(
|
192
|
+
|
193
|
+
'type' => 'select',
|
194
|
+
|
195
|
+
'div' => 'form-group',
|
196
|
+
|
197
|
+
'class' => 'form-control',
|
198
|
+
|
199
|
+
'options' => $posts));
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
echo $this->Form->input('Tag', array(
|
204
|
+
|
205
|
+
'div' => 'checkbox_wrap',
|
206
|
+
|
207
|
+
'type' => 'select',
|
208
|
+
|
209
|
+
'multiple' => 'checkbox',
|
210
|
+
|
211
|
+
'options' => $tag
|
212
|
+
|
213
|
+
));
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
if (!empty($tag_error)) {
|
218
|
+
|
219
|
+
echo '<div class="error-message tagerr">';
|
220
|
+
|
221
|
+
echo $tag_error[0];
|
222
|
+
|
223
|
+
echo '</div>';
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
?>
|
228
|
+
|
229
|
+
```
|