Codeigniter表单验证库“必填字段”,即使有值

时间:2018-07-23 00:39:44

标签: php codeigniter

从两个选择输入中获取数据库数据表明我的一个选择输入为空。它告诉我,实际上它具有值时是必需的。

这是我的javascript视图

heightForRowAt

JS

<div class="row entry">
    <div class="col-md-6">
        <div class="form-group">
            <label class="text-danger">Main Category</label>
            <select class="form-control" name="form-main-category-type" data-form-type="sample" id="form-main-category-type" data-target="form-sub-category-type" data-url="<?php echo base_url("dashboard/staff/inventory/find_sub_category"); ?>" required>
                <option value=""></option>
                <?php foreach($param_categories as $category) {?>
                <option value="<?php echo $category['main_category'];?>"><?php echo $category['main_category'];?></option>
                <?php } ?>
            </select>
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-group">
            <label class="text-danger">Sub Category</label>
            <select class="form-control" name="form-sub-category-type" data-form-type="sample" id="form-sub-category-type" data-url="<?php echo base_url("dashboard/staff/inventory/get_items"); ?>" required>
                <option value=""></option>

            </select>
        </div>
    </div>
</div>

这是我的控制器

<script>
    $(document).ready(function() {

        $('#dataTableInventory').DataTable();

        $.subCategoryHandler = {
            getSubCategories: function(obj) {
                $(obj).parents('.entry').find('select[name="'+obj.data('target')+'"] option').each(function(){$(this).remove();});
                $(obj).parents('.entry').find('select[name="'+obj.data('target')+'"]').append('<option value=""></option>');
                $.post(obj.data('url'), {main_category:$(obj).val()}, function(data) {
                    if (data.ret) {
                        $.each(data.sub_categories, function(key, val) {
                            $(obj).parents('.entry').find('select[name="'+$(obj).data('target')+'"]').append('<option value="'+ val +'">'+ val +'</option>');
                        });
                    }
                }, 'json');
            },
            getItems: function(obj) {
                console.log($('#form-main-category-type').val());
                console.log($(obj).val());
                $.post(obj.data('url'), {main_category:$('#form-main-category-type').val(),sub_category:$(obj).val()}, function(data) {
                    if (data.ret) {
                        console.log(data.item_data);
                    }
                }, 'json');
            }
        };

        $('#form-main-category-type').change(function() {
            $.subCategoryHandler.getSubCategories($(this));   
        });

        $('#form-sub-category-type').change(function() {
            $.subCategoryHandler.getItems($(this));   
        });

    });
</script>

我的控制器中的public function find_sub_category() { $this->load->library('form_validation'); $main_category = $this->input->post('main_category'); $this->form_validation->set_data(array('form-main-category-type'=>$main_category)); $this->form_validation->set_rules('form-main-category-type', 'Main Category', 'required'); if (!$this->form_validation->run()) { echo json_encode(array('ret'=>0)); return; } $staffInfo = $this->staffinfo_model->selectItem(array('member_idx'=>$this->session->userdata('member_index')),'department_group'); $items = $this->inventoryinfo_model->getItemsArray(array('main_category'=>$main_category, 'department_owner'=>$staffInfo->department_group), 'sub_category'); $sub_categories = array_unique(array_map(function($i) { return $i['sub_category']; }, $items)); echo json_encode(array('ret'=>1, 'sub_categories'=>$sub_categories)); } public function get_items() { $this->load->library('form_validation'); $main_category = $this->input->post('main_category'); $sub_category = $this->input->post('sub_category'); $this->form_validation->set_data(array('form-main-category-type'=>$main_category)); $this->form_validation->set_data(array('form-sub-category-type'=>$sub_category)); $this->form_validation->set_rules('form-main-category-type', 'Main Category', 'required'); $this->form_validation->set_rules('form-sub-category-type', 'Sub Category', 'required'); if (!$this->form_validation->run()) { echo validation_errors(); return; } $staffInfo = $this->staffinfo_model->selectItem(array('member_idx'=>$this->session->userdata('member_index')),'department_group'); $items = $this->inventoryinfo_model->getItemsArray(array('main_category'=>$main_category, 'sub_category'=>$sub_category, 'department_owner'=>$staffInfo->department_group)); var_dump($items); // echo json_encode(array('ret'=>1, 'item_data'=>$items)); } )方法有效,并且还可以进行表单验证。但是,当我运行get_items()时,它说的是find_sub_category(。我检查了我的两个数据,sub_category和main_category,但是都包含数据。我尝试对表单验证发表评论,它确实返回了一些数据,这意味着它们都具有价值。

现在为什么表单验证说实际上需要数据验证时是必需的?

1 个答案:

答案 0 :(得分:1)

更改

    $this->form_validation->set_rules('form-main-category-type', 'Main Category', 'required');

    $this->form_validation->set_rules('main_category', 'Main Category', 'required');