实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/27 0027 * Time: 下午 6:26 */ namespace app\Validate; use think\Validate; class EmpValidate extends Validate { protected $rule = [ 'name' => 'require|length:4,14', 'grade' => 'in:1,2,3', ]; protected $message = [ 'name.require' => '姓名不能为空', 'name.length' => '姓名长度4到14', 'grade.in' => '等级必须在1到3之间', ]; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/27 0027 * Time: 下午 5:34 */ namespace app\index\controller; use app\Validate\EmpValidate; use think\Controller; use think\Validate as ValidateSelf; class Validate extends Controller { public function ValidateData() { $rule = [ 'name' => 'require|length:4,14', 'grade' => 'in:1,2,3', ]; $data = [ 'name' => '1234', 'grade' => 0 ]; $message = [ 'name.require'=>'姓名不能为空', 'name.length'=>'姓名长度4到14', 'grade.in'=>'等级必须在1到3之间', ]; $res = $this->validate($data, $rule,$message); echo $res . " 直接输出这个信息<br><br><br><br>"; if (is_bool($res) && $res == true ){//这里仅仅用$res 是否 == true 判断很尴尬 全都不执行 这php有毒啊 echo " 验证成功"; } else { echo $res . ' 验证失败 '; } } public function ValidateSelf(){ $rule = [ 'name' => 'require|length:4,14', 'grade' => 'in:1,2,3', ]; $data = [ 'name' => '1234', 'grade' => 0 ]; $message = [ 'name.require'=>'姓名不能为空', 'name.length'=>'姓名长度4到14', 'grade.in'=>'等级必须在1到3之间', ]; $validate = ValidateSelf::make($rule,$message); $res = $validate->check($data); if (is_bool($res)&&$res==true){ return '验证通过'; }else{ return "验证失败".$validate->getError(); } } public function OutValidate(){ $data = [ 'name' => '1234', 'grade' => 1 ]; $empValidate = new EmpValidate(); $res = $empValidate->check($data); if (is_bool($res)&&$res==true){ return '验证通过'; }else{ return '验证失败'.$empValidate->getError(); } } }
运行实例 »
点击 "运行实例" 按钮查看在线实例