abstrak:<?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/21 * Time: 19:54 */ namespace app\validate; use
<?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/21 * Time: 19:54 */ namespace app\validate; use think\Validate; // 創(chuàng)建演員驗證器 class Actors extends Validate { // 創(chuàng)建驗證規(guī)則 protected $rule = [ 'name'=>'require|length:4,15', 'phone'=>'mobile', 'country'=>'require|length:2,30', 'birthday'=>'between:1,31', 'weight'=>'between:60,300', 'height'=>'between:100,200', ]; // 創(chuàng)建驗證信息 protected $message = [ 'name.require'=>'演員名稱不能為空', 'name.length'=>'演員名稱必須4-15個字符之間', 'phone.mobile'=>'手機(jī)號碼格式不正確', 'country.require'=>'演員國家不能為空', 'country.length'=>'演員國家必須是2-30個字符之間', 'birthday.between'=>'演員生日必須是1-31之間', 'weight.between'=>'演員體重必須是60-300之間', 'height.between'=>'演員盛高必須是100,200之間' ]; } ?> <?php /** * Created by PhpStorm. * User: Jason * Date: 2019/4/21 * Time: 20:02 */ namespace app\index\controller; // 引入驗證類 use app\validate\Actors; use think\Controller; class Actor extends Controller { // 驗證 public function vals() { // 實例化驗證類 $validate = new Actors; // 創(chuàng)建數(shù)據(jù) $data = [ 'name'=>'史提夫.史泰龍', 'phone'=>17688990987, 'country'=>'美國', 'birthday'=>'23', 'weight'=>90, 'height'=>188, ]; // 使用check方法驗證 if(!$validate->check($data)) { return $validate->getError(); } return '驗證成功'; } } ?>
Guru membetulkan:天蓬老師Masa pembetulan:2019-04-22 10:22:18
Rumusan guru:創(chuàng)建驗證的提示信息, 是比較麻煩的, 因為針對 每一個驗證規(guī)則都要設(shè)置, 如果你的需要不是太性化, 其實框架內(nèi)置的提示信息 基本上可以滿足大部分場景下的需求的