codeigniter

时间:2018-05-29 07:34:00

标签: php codeigniter validation codeigniter-3

我有一个用我自己的表单构建器构建的表单,我的表单构建器类似于WordPress的重力表单。除验证外,一切正常。每次我提交表单都会返回true。

特别是,我正在验证select字段,因为用户可以从Web检查器编辑值并提交值,这就是我在DB中检查值的原因,其中动态表单是DB中的保存值保存。 这是我的尝试

where $post is $_POST[] array, $postKey is fieldName, & $postValue is submitted value by user

public function validate()
{
    $post = $this->ci->input->post();
    //check form tempering
    if(empty($post)) 
        return false;

    $formId = $post['form_id'];
    //get form from db
    $data=[];
    $this->ci->db->select('formData');
    $this->ci->db->where('formId', $formId);
    $query = $this->ci->db->get(TBL_FORM_DATA);
    if($query->num_rows() > 0)
    {
        $data = $query->row();
    }
    if(empty($data) ) 
        return false;
    //unset submit button and 0 if occurs

    unset($post['submit']);
    unset($post['0']);

    //decode json
    $data = json_decode($data->formData,true);
    $fields = $data['field'];
    //debug($fields);
    //iterate post values and keys

    foreach ($post as $postKey => $postValue) 
    {
        //echo '<br>'.$postKey;
        if(strpos($postKey, '@'))
        {
            list($fieldName,$field_id) = explode('@', $postKey);

            $field_display_name = ucwords(str_replace('_', ' ', $fieldName));

            if(strpos($field_id,'|')) {
                list($field_id,$price) = explode('|', $field_id);
            }

            $field = $fields[$field_id];
            //debug($field['choice'],false);

            if(isset($field['choice']) && (isset($field['choice']['label']) && isset($field['choice']['value'])) )
            {
                /*if(!in_array($postValue, $field['choice']['value'])) 
                {*/
                    //$list = implode(',', $field['choice']['value']);

                    $this->ci->form_validation->set_rules($postKey,$field_display_name,'callback_check_field['.$list.']');
                    /*$this->ci->form_validation->set_rules($postKey, $field_display_name,"in_list[".$list."]",
                        array('in_list' => 'Invalid Selection. Please Select from available choices.')
                    );*/
                /*}*/
            }

            if(isset($field['required']) && $field['required']=='on'){
                $this->ci->form_validation->set_rules($postKey, $field_display_name,'required|trim');
            }

            //$duplicate = isset($field['no_duplicate']) ? true : false;
            //$this->ci->form_validation->set_rules('form_id',' ','required');
            //$this->ci->form_validation->set_rules('inkform_total',' ','required');    
        }
        $this->ci->form_validation->set_error_delimiters('<div class="clearfix"></div><p class="alert alert-danger">','</p>');

        return $this->ci->form_validation->run();

    }


    //echo validation_errors();

}

用于检查选择字段值的回调函数

function check_field($field,$list)
{ 
    if(!is_array($list)) {
        $list = explode(',', $list);
    }
    if(!in_array($field, $list)){
        $this->ci->form_validation->set_message('check_field','Invalid Post');
        return false;
    }else{
        return true;
    }

}

后来我只想访问validate()函数进行验证。

1 个答案:

答案 0 :(得分:1)

移动

System.Net.ServicePointManager.Expect100Continue = false;

外部 foreach