• CI框架, 参数验证


     /**
    * 统一API参数检验方法
    *
    * 调用示例 check_param(array('money' => array('required', 'integer', 'greater_than_equal_to[1]', 'less_than_equal_to[200]')));
    *
    * @access public
    * @param array $arr
    * @since 1.0
    * @return boolean
    */
    public function check_param($arr, $data = array(), $method = 'get')
    {
    /**
    * 设置要验证的请求数据
    */
    if (!empty($arr)) {
    $key_arr = array();
    $rule_arr = array();
    foreach ($arr as $key => $value) {
    $temp_arr = explode(",", $key);
    if (!is_array($value)) {
    $value = explode("|", $value);
    }
    $key_arr = array_merge($key_arr, $temp_arr);
    if (!empty($temp_arr)) {
    foreach ($temp_arr as $temp_value) {
    if (!empty($rule_arr[$temp_value])) {
    $rule_arr[$temp_value] = array_merge($rule_arr[$temp_value], $value);
    } else {
    $rule_arr[$temp_value] = $value;
    }
    }
    }
    }
    $key_arr = array_unique($key_arr);
    if (!empty($rule_arr)) {
    foreach ($rule_arr as $rule_key => $rule_value) {
    $rule_arr[$rule_key] = array_unique($rule_value);
    }
    }
    }
    if ($method === 'post' || $method === 'POST') {
    $request_data = $this->input->post($key_arr, TRUE);
    } else {
    $request_data = $this->input->get($key_arr, TRUE);
    }

    if ('get_post' == $method) {
    $request_data = [];
    foreach ($key_arr as $one_key) {
    $request_data[$one_key] = $this->input->get_post($one_key, TRUE);
    }
    }

    $raw_input_stream = $this->input->raw_input_stream;
    // $raw_input_stream = stripslashes($raw_input_stream);

    $input_stream = json_decode($raw_input_stream, TRUE);
    if (json_last_error() === JSON_ERROR_NONE) {
    foreach ($key_arr as $one_key) {
    if (NULL === $request_data[$one_key] and isset($input_stream[$one_key])) {
    $request_data[$one_key] = $input_stream[$one_key];
    }
    }
    }
    $this->form_validation->set_data($request_data);
    /**
    * 设置验证规则
    */
    if (!empty($rule_arr)) {
    $lang = $this->lang->line('txt_base_validation');
    foreach ($rule_arr as $rule_key => $rule_value) {
    $ts = $this->form_validation->set_rules($rule_key, '', $rule_value, array('required' => $lang[0]
    , 'integer' => $lang[1]
    , 'regex_match' => $lang[2]
    , 'greater_than' => $lang[3]
    , 'max_length' => $lang[4]
    , 'min_length' => $lang[5]
    ));
    }
    }
    /**
    * 开始验证
    */
    if (!$this->form_validation->run()) {
    //验证失败处理逻辑
    $errmsg = validation_errors(' ', ' ');
    if (!empty($arr) && !empty($data)) {
    foreach ($arr as $arr_key => $arr_value) {
    $errmsg = str_replace($arr_key, $data[$arr_key], $errmsg);
    }
    }
    $this->returnError($errmsg . $this->lang->line('txt_base_error_msg'), '400');
    return FALSE;
    }
    return $request_data;
    }
  • 相关阅读:
    或许因为缺少默认route配置而导致的的ping超慢,甚至timeout
    zabbix没有frontends目录
    jenkins自动部署到tomcat报错:ERROR: Publisher hudson.plugins.deploy.DeployPublisher aborted due to exception
    tomcat访问manager报404;server.xml中配置了Context path
    配置使用;yum安装slatstack的master,minion<at>centos6_x86_64
    jenkins报错;自定义工作目录;
    深入剖析Java中的装箱和拆箱
    探秘Java中的String、StringBuilder以及StringBuffer
    Java异常处理和设计
    JVM的内存区域划分
  • 原文地址:https://www.cnblogs.com/da-guang/p/6518328.html
Copyright © 2020-2023  润新知