本文整理汇总了PHP中think\Model::getError方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::getError方法的具体用法?PHP Model::getError怎么用?PHP Model::getError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类think\Model
的用法示例。
在下文中一共展示了Model::getError方法的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());
}
示例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());
}
示例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());
}