public function testSetTypeMsg() { $rule = ['name|名称' => 'require|max:25', 'age' => 'number|between:1,120', 'email' => 'email', ['sex', 'in:1,2', '性别错误']]; $data = ['name' => '', 'age' => 10, 'email' => '*****@*****.**', 'sex' => '3']; $validate = new Validate($rule); $validate->setTypeMsg('require', ':attribute必须'); $result = $validate->batch()->check($data); $this->assertFalse($result); $this->assertEquals(['name' => '名称必须', 'sex' => '性别错误'], $validate->getError()); }
public function testFill() { $data = ['username' => '', 'nickname' => 'nickname', 'phone' => ' 123456', 'hobby' => ['1', '2'], 'cityid' => '1', 'a' => 'a', 'b' => 'b']; $auto = ['user' => ['__option__' => ['value_fill' => ['username', 'password', 'phone'], 'exists_fill' => 'nickname'], 'username' => ['strtolower', 'callback'], 'password' => ['md5', 'callback'], 'nickname' => [[&$this, 'fillName'], 'callback', 'cn_'], 'phone' => function ($value, $data) { echo $value; return trim($value); }, 'ab' => ['a,b', 'serialize'], 'cityid' => ['1', 'ignore'], 'address' => ['address'], 'integral' => 0, ['reg_time', 'time', 'callback'], ['login_time', function ($value, $data) { return $data['reg_time']; }]]]; Config::set('auto', $auto); $result = Validate::fill($data, 'user'); $data['nickname'] = 'cn_nickname'; $data['phone'] = '123456'; $data['ab'] = serialize(['a' => 'a', 'b' => 'b']); $data['address'] = 'address'; $data['integral'] = 0; $data['reg_time'] = time(); $data['login_time'] = $data['reg_time']; unset($data['cityid'], $data['a'], $data['b']); $this->assertEquals($data, $result); }