PHP表单提交参数验证类(可修改)
生活随笔
收集整理的這篇文章主要介紹了
PHP表单提交参数验证类(可修改)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');/*** 表單驗(yàn)證類(lèi)(參考 原ci CI_Form_validation 而修改)* 自行擴(kuò)展里頭驗(yàn)證器*/
class CI_Form_validation {protected $CI;protected $_model;//屬性值及規(guī)則數(shù)組protected $_field_data = array();//錯(cuò)誤所提示的信息protected $_error_messages = array();//收集到的錯(cuò)誤protected $_error_array = array();//是否驗(yàn)證所有內(nèi)容protected $_is_vail_all = FALSE;//驗(yàn)證字段public $rule_field = array();// private $_validation_data = array();public function __get($key){return $this->_field_data[$key]['content'];}public function __set($key,$value){$this->_field_data[$key]['content'] = $value;}public function get_data(){$content = array();foreach ($this->_field_data as $key => $value) {$content[$key] = $value['content'];}return $content;}/*** 獲取有驗(yàn)證規(guī)則的數(shù)據(jù)* @return [type] [description]*/public function get_safe_data(){$content = array();foreach ($this->_field_data as $key => $value) {if(in_array($key, $this->rule_field))$content[$key] = $value['content'];}return $content;}/*** 屬性賦值* @param [type] $data [description]*/public function set_data($data){$this->_set_data($data);}/*** 屬性賦值* @param $data*/protected function _set_data($data){foreach ($data as $key => $value) {if(is_array($value)){foreach($value as $vkey => $vvalue){$value[$key.'.'.$vkey] = $vvalue;}$this->_set_data($value);}$this->_field_data[$key]['content'] = $value;}}/*** Constructor*/public function __construct($rules = array()){$this->CI =& get_instance();$this->_model = $this->CI;}/*** 設(shè)置規(guī)則* @param [type] $field 屬性* @param string $label 屬性名稱* @param array $rules 規(guī)則* @param [type] $error_message [description]*/public function set_rule($field, $label = '', $rules = array(), $error_message=null){$add_rule = false;if(strstr($field,'.')){$search_field = substr($field,0,strrpos($field,'.'));$fields = array_keys($this->_field_data);foreach($fields as $fk => $fv){if($fv == $search_field){$add_rule = true;}}}else{$add_rule = true;}if($add_rule){if(is_string($rules))$rules = explode('|', $rules);$this->_field_data[$field]['field'] = $field;$this->_field_data[$field]['label'] = $label;$this->_field_data[$field]['rules'] = $rules;if($error_message){foreach ($rules as $rule) {$this->_error_messages[$field][$rule] = $error_message;}}array_push($this->rule_field, $field);return $this;}}/*** 批量設(shè)置規(guī)則* @param Array $data [description]*/public function set_rules(Array $data){foreach ($data as $key => $value) {$this->set_rule($value[0],$value[1],$value[2]);}}/*** 驗(yàn)證所有屬性,并收集錯(cuò)誤* @return [type] [description]*/public function run(){$this->_validate_data($this->_field_data);//判斷是否驗(yàn)證通過(guò)if(count($this->_error_array)>0){return false;}return true;}/**** 驗(yàn)證所有屬性,并收集錯(cuò)誤* @param array $form_data*/protected function _validate_data($form_data = []){foreach($form_data as $key => $field) {if(!empty($field['rules'])){//非必需屬性,且為空,不做驗(yàn)證,返回trueif(!in_array('required', $field['rules']) && empty($field['content'])){$result = true;}//必需屬性,且為空,不做驗(yàn)證,返回falseelseif(in_array('required', $field['rules']) && empty($field['content'])){array_push($this->_error_array, $field['label'].'不得為空');$result = false;}// 屬性不為空,則驗(yàn)證else{$result = $this->_execute($key,$field['label'],$field['rules'],$field['content']);}// 驗(yàn)證到錯(cuò)誤,則不繼續(xù)往下驗(yàn)證if($this->_is_vail_all==false && $result == false){break;}}}}/*** Executes the Validation routines** @param array* @param array* @param mixed* @param int* @return mixed*/protected function _execute($field, $label, $rules, $content){//循環(huán)驗(yàn)證規(guī)則foreach ($rules as $rule){if($rule == 'required'){$result = true;continue;}$param = FALSE;if ( preg_match('/(.*?)\[(.*)\]/', $rule, $match)){$rule = $match[1];$param = $match[2];}if( method_exists($this->_model, $rule)){$result = $this->_model->$rule( $field, $content, $param, $this);}elseif( method_exists($this, $rule)){$result = $this->$rule($field, $content, $param);}else{log_message('debug', 'Unable to find callback validation rule: '.$rule);$result = FALSE;}// 驗(yàn)證到錯(cuò)誤,則不繼續(xù)往下驗(yàn)證if($this->_is_vail_all==false && $result == false){break;}}return $result;}public function get_label_by_field($field){return $this->_field_data[$field]['label'];}/*** 獲取最新的一條錯(cuò)誤信息* @return [type] [description]*/public function get_newone_error(){if(empty($this->_error_array)){return null;}else{return end($this->_error_array);}}/*** 添加錯(cuò)誤信息* @param [type] $msg [description]*/public function add_error($msg){array_push($this->_error_array, $msg);}/*** reset form* @return [type] [description]*/public function reset(){$this->_field_data = array();//錯(cuò)誤所提示的信息$this->_error_messages = array();//收集到的錯(cuò)誤$this->_error_array = array();//是否驗(yàn)證所有內(nèi)容$this->_is_vail_all = FALSE;//驗(yàn)證字段$this->rule_field = array();}/*** 以下方法為驗(yàn)證器**//*** 安全字段不做驗(yàn)證* @param [type] $field [description]* @param [type] $content [description]* @param [type] $param [description]* @return [type] [description]*/public function safe($field, $content, $param){return true;}/*** 驗(yàn)證最大長(zhǎng)度** @param [type] $field* [description]* @param [type] $content* [description]* @param [type] $param* [description]* @return [type] [description]*/public function max_length($field, $content, $param){if (mb_strlen($content) > $param) {$msg = $this->get_label_by_field($field) . '不得超過(guò)' . $param . '字符';$method_arr = explode(':', __METHOD__);$method = $method_arr[count($method_arr) - 1];$this->add_error($msg, $field, $method);return false;}return true;}/*** 驗(yàn)證最小長(zhǎng)度** @param [type] $field* [description]* @param [type] $content* [description]* @param [type] $param* [description]* @return [type] [description]*/public function min_length($field, $content, $param){if (mb_strlen($content) < $param) {$msg = $this->get_label_by_field($field) . '不得少于' . $param . '字符';$method_arr = explode(':', __METHOD__);$method = $method_arr[count($method_arr) - 1];$this->add_error($msg, $field, $method);return false;}return true;}/*** 驗(yàn)證是否為數(shù)字** @param [type] $field* [description]* @param [type] $content* [description]* @param [type] $param* [description]* @return [type] [description]*/public function numeric($field, $content, $param){if (! is_numeric($content)) {$msg = $this->get_label_by_field($field) . '必需為數(shù)字';$method_arr = explode(':', __METHOD__);$method = $method_arr[count($method_arr) - 1];$this->add_error($msg, $field, $method);return false;}return true;}/*** 驗(yàn)證參數(shù)是否在字符串中* @param [type] $field [description]* @param [type] $content [description]* @param [type] $param [description]* @return [type] [description]*/public function in_key_string($field, $content, $param){$arr = explode(',', $param);if(!in_array($content, $arr)){$msg = $this->get_label_by_field($field).'不合法';$this->add_error($msg);return false;}return true;}/*** 驗(yàn)證是否為日期格式 (類(lèi)似:2011-10-02)* @param [type] $field [description]* @param [type] $content [description]* @param [type] $param [description]* @return [type] [description]*/public function date_format($field, $content, $param){$pattern = '/^([1-2]\d{3})\-(0?[1-9]|10|11|12)\-([1-2]?[0-9]|0[1-9]|30|31)$/i';if(!preg_match($pattern, $content)){$msg = $this->get_label_by_field($field).'必需為正確的日期格式';$this->add_error($msg);return false;}return true;}/*** 驗(yàn)證時(shí)間格式(類(lèi)似 08:00:00,24小時(shí)制度,從00:00:00到23:59:59)*/public function time_format($field, $content, $param){$pattern = '/^([0-1]\d|2[0-3]):([0-5]\d):([0-5]\d)$/i';if (! preg_match($pattern, $content)) {$msg = $this->get_label_by_field($field) . '必需為正確的時(shí)間格式';$method_arr = explode(':', __METHOD__);$method = $method_arr[count($method_arr) - 1];$this->add_error($msg, $field, $method);return false;}return true;}/*** 驗(yàn)證郵箱* @param [type] $field [description]* @param [type] $content [description]* @param [type] $param [description]* @return [type] [description]*/public function email($field, $content, $param){$pattern = "/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix";if(!preg_match($pattern, $content)){$msg = $this->get_label_by_field($field).'必需為正確的格式';$this->add_error($msg);return false;}return true;}/*** 身份證驗(yàn)證* (1、15位或18位,如果是15位,必需全是數(shù)字。2、如果是18位,最后一位可以是數(shù)字或字母Xx,其余必需是數(shù)字。)* @param [type] $field [description]* @param [type] $content [description]* @param [type] $param [description]* @return [type] [description]*/public function card_id($field, $content, $param){$pattern = "/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/";if(!preg_match($pattern, $content)){$msg = $this->get_label_by_field($field).'必需為正確的格式';$this->add_error($msg);return false;}return true;}/*** 驗(yàn)證手機(jī)號(hào)碼* @param [type] $field [description]* @param [type] $content [description]* @param [type] $param [description]* @return [type] [description]*/public function phone($field, $content, $param){$pattern = "/^1[3|4|5|7|8][0-9]{9}$/";if(!preg_match($pattern, $content)){$msg = $this->get_label_by_field($field).'必需為正確的格式';$this->add_error($msg);return false;}return true;}/*** 驗(yàn)證qq號(hào)碼* @param [type] $field [description]* @param [type] $content [description]* @param [type] $param [description]* @return [type] [description]*/public function qq($field, $content, $param){$pattern = "/^[1-9]\d{4,}$/";if(!preg_match($pattern, $content)){$msg = $this->get_label_by_field($field).'必需為正確的格式';$this->add_error($msg);return false;}return true;}/*** 固定電話* @param [type] $field [description]* @param [type] $content [description]* @param [type] $param [description]* @return [type] [description]*/public function tel($field, $content, $param){$pattern = "/^\d{2,5}-\d{7,8}(-\d{1,})?$/";if(!preg_match($pattern, $content)){$msg = $this->get_label_by_field($field).'必需為正確的格式';$this->add_error($msg);return false;}return true;}public function city_required($field, $content, $param){if(empty($content) || $content=='9999'){$msg = $this->get_label_by_field($field).'必需選擇到城市';$this->add_error($msg);return false;}return true;}/*** 驗(yàn)證最大值,包含等于*/public function max($field, $content, $param){if (intval($content) > intval($param)) {$msg = $this->get_label_by_field($field) . '不得大于' . $param . '';$method_arr = explode(':', __METHOD__);$method = $method_arr[count($method_arr) - 1];$this->add_error($msg, $field, $method);return false;}return true;}/*** 驗(yàn)證最小值,包含等于*/public function min($field, $content, $param){if (intval($content) < intval($param)) {$msg = $this->get_label_by_field($field) . '不得小于' . $param . '';$method_arr = explode(':', __METHOD__);$method = $method_arr[count($method_arr) - 1];$this->add_error($msg, $field, $method);return false;}return true;}}
用法:先引入上面文件,在調(diào)用,以下為調(diào)用方法。
/** =====數(shù)據(jù)驗(yàn)證===== */$this->load->library('form_validation');$rules = [['otaOrderId','訂單標(biāo)號(hào)','required'],['supplierId','供應(yīng)商id','required'],['supplierPhone','供應(yīng)商手機(jī)號(hào)','required|phone'],['contact','訂單聯(lián)系人','required'],['contact.phone','訂單聯(lián)系人手機(jī)號(hào)','required|phone'],['contact.name','訂單聯(lián)系人姓名','required'],['items','訂單產(chǎn)品','required'],['items.id','合作方產(chǎn)品ID','required'],['items.sku','合作方產(chǎn)品sku','required'],['items.name','產(chǎn)品名稱','required'],['items.props','產(chǎn)品sku屬性','required'],['items.qty','下單數(shù)量','required|numeric'],['items.extra.name','產(chǎn)品額外信息字段名','required'],['items.extra.content','產(chǎn)品額外信息字段值','required'],];// 設(shè)置表單規(guī)則$this->form_validation->set_rules($rules);// 給表單賦值$this->form_validation->set_data($body);//表單驗(yàn)證是否符合規(guī)則$result = $this->form_validation->run();if( $result === false){$error = $this->form_validation->get_newone_error();$this->output(self::PARAM_IS_INVALID,$error);}/** =====數(shù)據(jù)驗(yàn)證結(jié)束=====*/?
總結(jié)
以上是生活随笔為你收集整理的PHP表单提交参数验证类(可修改)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 入股不亏什么意思
- 下一篇: 频繁使用借呗影响房贷吗