LoginSignup
0
0

More than 5 years have passed since last update.

テーブルに記述するvalidationDefaultのdateには気を付ける

Last updated at Posted at 2018-10-30

はじめに

コントローラーでValidation::dateを使ってテストした時と
newEntityでバリデーションした際では渡す変数の型には違いがあった
4時間くらい悩んだ

この値がPOST送信されてくると想定

$v="2018-10-31";

最初のバリデーションクラスのテスト

//これはちゃんとバリデーションが通る
$dateVlidation = CustomValidation::date($v);

実際のバリデーションの使用時(バリデーションエラー)

//Tableにバリデーション設定
public function validationDefault(Validator $validator)
{
        $validator
            ->requirePresence('entry_date')
            ->notEmpty('entry_date', __('日付を入力してください'))
            ->date('entry_date', __('正しい日付のフォーマットで入力してください'));
        return $validator;

}

//Controllerで
public function someFunction(){
$this->SomeTable= TableRegistry::get('SomeTable');

$data['entry_date'] = $v;

//(これはバリデーションが通らない)
$Some = $this->SomeTable->newEntity($data);

}

実際のバリデーションの使用時(バリデーションエラーなし)


//Controller
use Cake\I18n\Time;

public function someFunction(){
$this->SomeTable= TableRegistry::get('SomeTable');

//Timeでパースしてやらなきゃいけない
$data['entry_date'] = Time::parse($v);

//(これはちゃんとバリデーションが通る)
$Some = $this->SomeTable->newEntity($data);

}

EntityではValidateする際にTime型から値を変換してValidateしてると推測される
疲れた、、、。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0