<?php
/**
* 表单验证工具类
* User: Eden
* Date: 19-4-26 上午9:23
*/
namespace Common\Util;
use Vendor\Func\Json;
use Vendor\Func\Verify;
class ValidUtil extends CommonUtil {
/**
* @param $regulations
* @param $params
* @return bool
*/
public static function each($regulations,$params) {
//$regulations = [
// 'name' => [
// 'required' => '请先写姓名|10001',
// ],
// 'telephone' => [
// 'required' => '请先写手机号|10001',
// 'telephone' => '请先写正确的手机号|10002',
// ],
// 'id_number' => [
// 'required' => '请填写身份证号|10001',
// 'id_card' => '请填写正确的身份证号|10001',
// ],
// 'department_id' => [
// 'required' => '请选择科室|10001',
// ],
// 'uid' => [
// 'required' => '缺少用户id|10001',
// ],
//];
$json = new Json();
foreach ($regulations as $param => $regulation) {
foreach ($regulation as $rule => $tips) {
if (((string)$rule === 'required') && !$params[$param]) {
$tips = explode('|',$tips);
$json->printOutError($tips[0],$tips[1] ?:'10001');
break;
}
if (((string)$rule === 'telephone') && !Verify::checkMobile($params[$param])) {
$tips = explode('|',$tips);
$json->printOutError($tips[0],$tips[1] ?:'10001');
break;
}
if (((string)$rule === 'id_card') && !Verify::checkIDCard($params[$param])) {
$tips = explode('|',$tips);
$json->printOutError($tips[0],$tips[1] ?:'10001');
break;
}
}
}
return true;
}
}
使用如下,
$regulations = [
'name' => [
'required' => '请填写姓名|10001',
],
'telephone' => [
'required' => '请填写手机号|10001',
'telephone' => '请填写正确的手机号|10002',
],
'id_number' => [
'required' => '请填写身份证号|10001',
'id_card' => '请填写正确的身份证号|10001',
],
'department_id' => [
'required' => '请选择科室|10001',
],
'uid' => [
'required' => '缺少用户id|10001',
],
];
$params = $_POST;
ValidUtil::each($regulations,$params);
通用的验证,就交给它。
根数据库交互的验证,在下面的步骤中,继续进行。