タイトルの通りですが、一つのFormでSubmitした際、まったく別のFormでもバリデーションが反応してしまって困っています。例えば記事投稿ページのPost.titleというフィールドと、全ページ共通部分のタイトル検索フィールドPost.titleなどは同一ページ上に存在します。
これを回避する手段はあるでしょうか?お手数ですがご教授頂ければ幸いです。
<追記>
ありがとうございます。
addページでtitleがnullの状態で、Submitするとaddページ、検索フォームともに”このフィールドは入力必須です”のメッセージが出力されてしまう状態です。
actionが異なっているのにバリデーションが反応してしまうこと自体がおかしいのでしょうか?
searchはsearch pluginを使用しています。
お二人に教えていただいた内容も試してみましたが、うまく設定できていないのか機能してくれません。。
posts/add (新規記事投稿)
posts/search (記事タイトル検索)
Post.php <Model>
public $actsAs = array('Search.Searchable'); public $validate = array( 'title' => array( 'rule' => 'notBlank', 'message' => 'このフィールドは入力必須です。' ) ); public $filterArgs = array( array('name' => 'title', 'type' => 'like') }
PostsController.php
public function add() { $this->loadModel('Category', 'Tag'); // add to categories table $this->set('posts', $this->Category->find('list', array('fields' => 'Category.name'))); // tag $this->set('tag', $this->Tag->find('list')); if ($this->request->is('post')) { $this->request->data['Post']['user_id'] = $this->Auth->user('id'); #$this->Post->create(); if ($this->Post->saveAll($this->request->data)) { $this->Session->setFlash(__('Your post has been saved.'), 'alert', array( 'plugin' => 'BoostCake', 'class' => 'alert-success' )); return $this->redirect(array('action' => 'index')); } // tagが2つ以上選択されていない場合のエラーメッセージを取得 $errors = $this->Post->validationErrors; if (!empty($errors)) $this->set('tag_error', $errors['Tag']); $this->Session->setFlash(__('Unable to add your post.'), 'alert', array( 'plugin' => 'BoostCake', 'class' => 'alert-danger' )); } } public function search() { #$this->Post->recursive = 2; $this->Prg->commonProcess(); $conditions = $this->Post->parseCriteria($this->Prg->parsedParams()); if (!empty($conditions)) { $this->set('posts', $this->paginate(array( $conditions )) ); } }
sidebar.ctp(search)
<?php echo $this->Form->create('Post', array( 'novalidate' => true, 'url' => array_merge(array('action' => 'search'), $this->params['pass']) )); echo $this->Form->input('Post.title', array('div' => 'form-group', 'class' => 'form-control')); echo $this->Form->end(); ?>
add.ctp(create)
<?php echo $this->Form->create('Post', array('type' => 'file', 'novalidate' => true)); echo $this->Form->input('title', array('class' => 'form-control', 'div' => 'form-group')); echo $this->Form->input('body', array('rows' => '3', 'class' => 'form-control', 'div' => 'form-group')); echo $this->Form->input('category_id', array( 'type' => 'select', 'div' => 'form-group', 'class' => 'form-control', 'options' => $posts)); echo $this->Form->input('Tag', array( 'div' => 'checkbox_wrap', 'type' => 'select', 'multiple' => 'checkbox', 'options' => $tag )); if (!empty($tag_error)) { echo '<div class="error-message tagerr">'; echo $tag_error[0]; echo '</div>'; } ?>
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。