###質問概要(追記あり)
CakePHPのバリデーションについて
フォームヘルパーから送られたパスワードの入力値(inputpass)とパスワード確認の入力値(inputpass2)をバリデーションのsameAsルールを使って一致しているかをチェックしたいのですが、
Error: TestsHelper could not be found.
というエラーがtest1ページで出力されて使えません。
==>上記は追記1で消えました。が、新たに
Error: Table "App\Model\Table\TestsTable" is not associated with "validationErrors"
というエラーが出ています。
下記に挙げるコードのどこが間違っているか、あるいは、
バリデーションの書き方をご存知の方がいればご指摘頂きたく願います。
よろしくお願いします。
###前提(追記あり)
- SQLデータベースについて
CakePHPのSQL接続は完了し
Testsテーブルに
プライマリーキー、A_I_、のINTカラム「id」、
VARCHARカラム「inputpass」
がそれぞれ設定してあるとします。
- ページ遷移について
indexページから、test1ページにフォームの値をPOSTで送って
test1ページで値のチェックをするとします。
そして、saveメソッド、レコード追加はtest2ページでするものとして、
test1ページでは、値のチェック、エラー表示のみを行うものとします。
- モデルのテーブルファイル testsTable.php
testsTable.php
1<?php 2namespace App\Model\Table; 3use Cake\ORM\Table; 4 5class TestsTable extends Table{ 6 public function sameAs($a,$b) { 7 $v1 = array_shift($a); 8 $v2 = array_shift($b); 9 return $v1 == $v2; 10} 11 public $validator = [ 12 'inputpass' => [ 13 'alphaNumeric' => [ 14 'rule' => 'alphaNumeric', 15 'message' => '半角英数字のみで入力してください' 16 ], 17 'minLength' => [ 18 'rule' => ['minLength',6], 19 'message' => '6文字以上で入力してください' 20 ] 21 ], 22 'inputpass2' => [ 23 'alphaNumeric' => [ 24 'rule' => 'alphaNumeric', 25 'message' => '半角英数字のみで入力してください' 26 ], 27 'minLength' => [ 28 'rule' => ['minLength',6], 29 'message' => '6文字以上で入力してください' 30 ] 31 'sameAs' => [ 32 'rule' => ['sameAs','inputpass','inputpass2'], 33 'message' => 'パスワードが一致しません' 34 ] 35 ] 36]; 37} 38
- コントローラ TestsController.php
TestsController.php
1<?php 2namespace App\Controller; 3 4class TestsController extends AppController { 5 public function initialize(){ 6 $this->name = 'Tests'; 7 $this->viewBuilder()->autoLayout(true); 8 $this->viewBuilder()->layout('tests'); 9 } 10 public function index(){ 11 $this->set('entity',$this->Tests->newEntity()); 12 } 13 public function test1(){ 14 if($this->request->isPost()){ 15 $this->Tests->set($this->request->data); 16 } 17 public function test2(){ 18 if ($this->request->is('post')){ 19 $hoge = $this->Tests->newEntity($this->request->data); 20 $this->Tests->save($hoge); 21 } 22 23} 24} 25
コントローラTestsController.php追記1
TestsController.php
1<?php 2namespace App\Controller; 3 4class TestsController extends AppController { 5 public function initialize(){ 6 $this->name = 'Tests'; 7 $this->viewBuilder()->autoLayout(true); 8 $this->viewBuilder()->layout('tests'); 9 } 10 public function index(){ 11 $this->set('entity',$this->Tests->newEntity()); 12 } 13 public function test1(){ 14 if($this->request->isPost()){ 15 $this->Tests->set($this->request->data); 16 $this->Tests->set($hoge); 17 }else{ 18 $error = $this->Tests->validationErrors; 19 $this->set('error',$error); 20 } 21 } 22 public function test2(){ 23 if ($this->request->is('post')){ 24 $hoge = $this->Tests->newEntity($this->request->data); 25 $this->Tests->save($hoge); 26 } 27 28} 29} 30
- テンプレートファイル test1.ctp
test1.ctp
1<?php 2$error = array(); 3if($this->request->isPost()){ 4$this->Tests->set($this->request->data); 5$inputpass = $this->request->data('inputpass'); 6$inputpass2 = $this->request->data('inputpass2'); 7if($this->Tests->validates()){ 8header("http://hoge/test2",true,303); 9exit(); 10}else{ 11$error = $this->Tests->validationErrors; 12} 13} 14if($this->request->isPost() && !is_null($error)){ ?> 15<?=$this->Form->error('inputpass') ?> 16<?=$this->Form->error('inputpass2') ?> 17<?php } ?> 18<br> 19<a href="http://hoge/index.ctp">戻る</a> 20
テンプレートtest1.ctp追記1
test1.ctp
1<?php 2if($this->request->isPost()){ 3$this->Tests->set($this->request->data); 4$inputpass = $this->request->data('inputpass'); 5$inputpass2 = $this->request->data('inputpass2'); 6if(is_null($error){ 7header("http://hoge/test2",true,303); 8exit(); 9} 10} 11if($this->request->isPost() && !is_null($error)){ ?> 12<?=$this->Form->error('inputpass') ?> 13<?=$this->Form->error('inputpass2') ?> 14<?php } ?> 15<br> 16<a href="http://hoge/index.ctp">戻る</a> 17
###自分で調べたこと
持っているCakePHPの参考書を読み倒してみると、バリデーションメソッドsameAsというのが、存在する様に書いてあるのですが、
あまり詳しい記述がなく
そのままだと使えなかったので、
今は自分でsameAsを定義して使おうとしています。
CakePHPの記事を検索エンジンでもかなり調べたのですが、
CakePHP自体の日本語記事が少ないこともあって、
解決できるものはありませんでした。
Error: UsersHelper could not be found.と出力されますが、
ヘルパーを用意しても解決しないので、
そもそもヘルパーを用意しなくても解決できる書き方が他にあって、
自分の書き方が間違っているのではないかと考えています。
==>上記は追記1により解決しました。
新たに出ている
Error: Table "App\Model\Table\TestsTable" is not associated with "validationErrors"
というエラー出力ですが、テーブルファイルの記述が間違っているのではないかと自分では考えています。
バリデーションを入力値チェックのみのページで使う書き方をご存知の方がいればご指摘してもらえると大変助かります。
よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/03 04:03
2017/02/03 04:15 編集
2017/02/03 04:11
2017/02/03 04:22
2017/02/03 04:33
2017/02/03 05:48 編集
2017/02/03 06:49