[精选]Laravel 中自定义 验证,例如身份证号验证

Laravel框架
368
0
0
2022-11-09

laravel 5.5之后,您可以创建自己的自定义验证规则对象。

要创建新规则,只需运行Artisan命令:

php artisan make:rule Id

laravel将把新的rule类放在app/Rules目录中

自定义对象验证规则的示例可能类似于:

<?php
namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class Id implements Rule
{
    private $message;

    /**
     * Determine if the validation rule passes.
     *
     * @param string $attribute
     * @param mixed  $value
     *
     * @return bool
     */ 
    public function passes($attribute, $value)
    {
        if(!$this->isValidCard($value)){
            $this->message = '身份证格式错误,请输入正确的身份证.';
            return false;
        }

        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */ 
    public function message()
    {
        return $this->message;
    }

    /**
     * 身份证验证
     * @param $id
     * @return bool
     */ 
    private function isValidCard($id) {
        if(18 != strlen($id)) {
            return false;
        }
        $weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
        $code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
        $mode = 0;
        $ver = substr($id, -1);
        if($ver == 'x') {
            $ver = 'X';
        }
        foreach($weight as $key => $val) {
            if($key == 17) {
                continue;
            }
            $digit = intval(substr($id, $key, 1));
            $mode += $digit * $val;
        }
        $mode %= 11;
        if($ver != $code[$mode]) {
            return false;
        }
        list($month, $day, $year) = self::getMDYFromCard($id);
        $check = checkdate($month, $day, $year);
        if(!$check) {
            return false;
        }
        $today = date('Ymd');
        $date = substr($id, 6, 8);
        if($date >= $today) {
            return false;
        }
        return true;
    }

    private  function getMDYFromCard($id) {
        $date = substr($id, 6, 8);
        $year = substr($date, 0, 4);
        $month = substr($date, 4, 2);
        $day = substr($date, 6);
        return [$month, $day, $year];
    }
}

//在控制器里面引入:

$rules = [
    'buyer_name' => ['regex:/^[\p{Han}|\w]{2,30}$/u'],
    'buyer_id_number' => ['required', new Id],
    'buyer_telephone' => 'required|unique:investor',
];

Laravel 中自定义 验证,例如身份证号验证