當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Model::validate方法代碼示例

本文整理匯總了PHP中think\Model::validate方法的典型用法代碼示例。如果您正苦於以下問題:PHP Model::validate方法的具體用法?PHP Model::validate怎麽用?PHP Model::validate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在think\Model的用法示例。


在下文中一共展示了Model::validate方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: testValidate

 public function testValidate()
 {
     $model = new Model('', $this->getConfig());
     $data = $_POST = ['username' => 'username', 'nickname' => 'nickname', 'password' => '123456', 'repassword' => '123456', 'mobile' => '13800000000', 'email' => 'abc@abc.com', 'sex' => '0', 'age' => '20', 'code' => '1234'];
     $validate = ['__pattern__' => ['mobile' => '/^1(?:[358]\\d|7[6-8])\\d{8}$/', 'require' => '/.+/'], '__all__' => ['code' => function ($value, $data) {
         return '1234' != $value ? 'code error' : true;
     }], 'user' => [['username', [&$this, 'checkName'], '用戶名長度為5到15個字符', 'callback', 'username'], ['username', function ($value, $data) {
         return 'admin' == $value ? '此用戶名已被使用' : true;
     }], 'nickname' => ['require', '請填昵稱'], 'password' => ['[\\w-]{6,15}', '密碼長度為6到15個字符'], 'repassword' => ['password', '兩次密碼不一到致', 'confirm'], 'mobile' => ['mobile', '手機號錯誤'], 'email' => ['validate_email', '郵箱格式錯誤', 'filter'], 'sex' => ['0,1', '性別隻能為為男或女', 'in'], 'age' => ['1,80', '年齡隻能在10-80之間', 'between'], '__option__' => ['scene' => ['add' => 'username,nickname,password,repassword,mobile,email,age,code', 'edit' => 'nickname,password,repassword,mobile,email,sex,age,code'], 'value_validate' => 'email', 'exists_validate' => 'password,repassword,code', 'patch' => true]]];
     \think\Config::set('validate', $validate);
     $result = $model->validate('user.add')->create();
     var_dump($model->getError());
     $this->assertEmpty($model->getError());
     unset($data['password'], $data['repassword']);
     $data['email'] = '';
     $result = $model->validate('user.edit')->create($data);
     $this->assertEmpty($model->getError());
     // 測試帶.和*的鍵名
     $data = ['code' => '', 'name' => ['a' => '', 'b' => ''], 'sku' => [0 => [0 => ['item' => 'item', 'price' => ''], 1 => ['item' => 'item2', 'price' => '']]]];
     $test = ['code' => function ($value, $data) {
         return empty($value) ? ['code' => 'not empty'] : true;
     }, 'name.*' => ['/.+/', 'not empty'], 'sku.*.*.price' => ['/\\d+/', 'mast int'], '__option__' => ['patch' => true]];
     $result = $model->validate($test)->create($data);
     $msg = ['code' => 'not empty', 'name.a' => 'not empty', 'name.b' => 'not empty', 'sku.0.0.price' => 'mast int', 'sku.0.1.price' => 'mast int'];
     $this->assertEquals($msg, $model->getError());
     unset($test['__option__']['patch']);
     $result = $model->field('code')->validate($test)->create($data);
     $this->assertEquals(['code' => 'not empty'], $model->getError());
 }
開發者ID:Lofanmi,項目名稱:think,代碼行數:29,代碼來源:modelTest.php

示例2: testValidate

 public function testValidate()
 {
     $model = new Model('', $this->getConfig());
     $data = ['username' => 'username', 'nickname' => 'nickname', 'password' => '123456', 'repassword' => '123456', 'email' => 'abc@abc.com', 'sex' => '0', 'age' => '20', 'code' => '1234'];
     $validate = [['username', 'length:5,15', '用戶名長度為5到15個字符'], ['nickname', 'require', '請填昵稱'], ['password', '[\\w-]{6,15}', '密碼長度為6到15個字符'], ['repassword', 'confirm:password', '兩次密碼不一到致'], ['email', 'filter:validate_email', '郵箱格式錯誤'], ['sex', 'in:0,1', '性別隻能為為男或女'], ['age', 'between:1,80', '年齡隻能在10-80之間']];
     $result = $model->validate($validate)->create($data);
     $this->assertEmpty($model->getError());
 }
開發者ID:cnzin,項目名稱:think,代碼行數:8,代碼來源:modelTest.php

示例3: testValidate

 public function testValidate()
 {
     $model = new Model('', $this->getConfig());
     $data = $_POST = ['username' => 'username', 'nickname' => 'nickname', 'password' => '123456', 'repassword' => '123456', 'mobile' => '13800000000', 'email' => 'abc@abc.com', 'sex' => '0', 'age' => '20', 'code' => '1234'];
     $validate = ['__pattern__' => ['mobile' => '/^1(?:[358]\\d|7[6-8])\\d{8}$/', 'require' => '/.+/'], '__all__' => ['code' => function ($value, $data) {
         return '1234' != $value ? 'code error' : true;
     }], 'user' => [['username', [&$this, 'checkName'], '用戶名長度為5到15個字符', 'callback', 'username'], ['username', function ($value, $data) {
         return 'admin' == $value ? '此用戶名已被使用' : true;
     }], 'nickname' => ['require', '請填昵稱'], 'password' => ['[\\w-]{6,15}', '密碼長度為6到15個字符'], 'repassword' => ['password', '兩次密碼不一到致', 'confirm'], 'mobile' => ['mobile', '手機號錯誤'], 'email' => ['validate_email', '郵箱格式錯誤', 'filter'], 'sex' => ['0,1', '性別隻能為為男或女', 'in'], 'age' => ['1,80', '年齡隻能在10-80之間', 'between'], '__option__' => ['scene' => ['add' => 'username,nickname,password,repassword,mobile,email,age,code', 'edit' => 'nickname,password,repassword,mobile,email,sex,age,code'], 'value_validate' => 'email', 'exists_validate' => 'password,repassword,code', 'patch' => true]]];
     Config::set('validate', $validate);
     $result = $model->validate('user.add')->create();
     $this->assertEmpty($model->getError());
     unset($data['password'], $data['repassword']);
     $data['email'] = '';
     $result = $model->validate('user.edit')->create($data);
     $this->assertEmpty($model->getError());
 }
開發者ID:Aoiujz,項目名稱:think,代碼行數:17,代碼來源:modelTest.php


注:本文中的think\Model::validate方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。