多个动态字段代码验证器的表单验证

时间:2018-09-08 09:19:30

标签: php codeigniter

我正在使用codeigniter HMVC。单选按钮是从数据库生成的。我在[]中放了一个ID。因此,该ID也将作为外键保存到数据库中。

这是我的观点

<form id="demo-form2" data-parsley-validate class="form-horizontal form-label-left" action="<?= $form_location ?>" method="post">
                <table class="table">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th class="col-md-4">Question</th>
                        <th>Penilaian </th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    $count = 0;
                    foreach ($que->result() as $item) {
                        $question = $item->borang_question;
                        $borang_id = $item->id;
                        $count++;
                        ?>
                        <tr style="background: #f6f6f6">
                            <th scope="row"><?= $count ?></th>
                            <td><?= $question ?></td>
                            <td></td>
                        </tr>
                        <?php
                        $this->load->module('borang_master');
                        $mysql_query = "select borang_master.*, subject_master.subject_title 
                                      from borang_master
                                      left join subject_master
                                      on borang_master.subject_id = subject_master.id
                                      where borang_master.parent_id = '$borang_id'";
                        $query_for_child = $this->borang_master->_custom_query($mysql_query);
                        foreach ($query_for_child->result() as $row) {
                            $que = $row->borang_question;
                            $item_id = $row->id;

                            ?>
                            <tr>
                                <th scope="row"></th>
                                <td>- <?= $que ?></td>
                                <td>
                                    <div class="col-md-2">
                                        <input type="radio" <?= set_checkbox('score['.$item_id.']', '1'); ?> value="1" id="optionsRadios1" name="score[<?= $item_id ?>]"> 1
                                    </div>
                                    <div class="col-md-2">
                                        <input type="radio" <?= set_checkbox('score['.$item_id.']', '3'); ?> value="3" id="optionsRadios2" name="score[<?= $item_id ?>]"> 3
                                    </div>
                                    <div class="col-md-2">
                                        <input type="radio" <?= set_checkbox('score['.$item_id.']', '5'); ?> value="5" id="optionsRadios3" name="score[<?= $item_id ?>]"> 5
                                    </div>
                                    <div class="col-md-2">
                                        <input type="radio" <?= set_checkbox('score['.$item_id.']', '8'); ?> value="8" id="optionsRadios4" name="score[<?= $item_id ?>]"> 8
                                    </div>
                                    <div class="col-md-2">
                                        <input type="radio" <?= set_checkbox('score['.$item_id.']', '13'); ?> value="13" id="optionsRadios5" name="score[<?= $item_id ?>]"> 13
                                    </div>
                                </td>
                            </tr>

                            <?php

                        }
                    }
                    ?>
                    <tr style="background: #f6f6f6">
                        <th scope="row"></th>
                        <td>Harapan</td>
                        <td>
                            <textarea type="text" rows="10" id="note" name="note" class="form-control col-md-7 col-xs-12"><?= $note ?></textarea>
                        </td>
                    </tr>
                    </tbody>
                </table>

                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-1">
                        <button type="submit" class="btn btn-primary" name="submit" id="submit" value="Submit">
                            Submit
                        </button>
                        <button type="submit" class="btn btn-default" name="submit" id="submit" value="Cancel">
                            Cancel
                        </button>
                    </div>
                </div>
            </form>

这是我的控制器

function create(){

    $this->load->library('session');
    $this->load->module('site_security');
    $this->load->module('timedate');
    $this->site_security->_make_sure_is_admin();

    $update_id = $this->uri->segment(3);
    $submit = $this->input->post('submit', TRUE);

    if ($submit == "Cancel"){
        redirect('borang_input/create/'.$update_id);
    }

    if ($submit == "Submit"){
        //Process the form
        $this->load->library('form_validation');





        $this->form_validation->set_rules('score', 'Nilai', 'required',
            array('required' => 'Mohon isikan semua kolom Nilai')
        );



        if ($this->form_validation->run() == TRUE){

            $mysql_query = "select * from subject_master where id = '$update_id'";
            $query = $this->_custom_query($mysql_query);
            foreach ($query->result() as $item) {
                $status_after = $item->aft;
                $status_before = $item->bef;
            }

            if ($status_after == 1) {
                $status = 1;
            } elseif ($status_before == 1) {
                $status = 0;
            } else {
                $status = 1;
            }

            foreach ($this->input->post('score', TRUE) as $key => $val) {
                $userid = $this->site_security->_get_user_id();
                $borang_master_id = $key;
                $score = $val;


                echo $borang_master_id.'.';
                echo $score.'.';
                echo $userid.'.';
                echo $status;
                echo '<br>';
            }


            $note = $this->input->post('note', TRUE);
            echo $note;
            die();

            //insert a new file
            $this->_insert($data);
            $update_id      = $this->get_max();//get the ID of the new item
            $flash_msg      = "The data was successfully added.";
            $value          = '<div class="alert alert-success" role="alert">'.$flash_msg.'</div>';
            $this->session->set_flashdata('item', $value);
            redirect('borang_input/create/'.$update_id);

        }
    }

    $data['note'] = $this->input->post('note', TRUE);
    $data['inputscore'] = $this->input->post('score[]', TRUE);



    $mysql_query = "select subject_master.*, borang_master.* 
                    from subject_master
                    left join borang_master
                    on subject_master.id = borang_master.subject_id
                    where (subject_master.bef = 1 OR subject_master.aft = 1) 
                    AND borang_master.parent_id = 0
                    AND subject_master.id = '$update_id'";

    $data['que'] = $this->_custom_query($mysql_query);

    $data['headline'] = "Borang Penilaian";

    $data['userlevel'] = $this->site_security->_get_user_level();
    $data['userid'] = $this->site_security->_get_user_id();

    $data['use_daterangepicker'] = TRUE;
    $data['update_id']      = $update_id;
    $data['flash']          = $this->session->flashdata('item');
    $data['view_file']      = "create";
    $this->load->module('templates');
    $this->templates->admin($data);
}

对我来说,最大的问题是表单验证。我如何利用codeigniter表单验证来确保所有单选按钮均已填充。这都是必需的。

谢谢。

0 个答案:

没有答案