Codeigniter表单验证错误显示在同一页面上的另一个表单上

时间:2016-12-09 17:53:25

标签: javascript php jquery forms codeigniter

所以在我开始之前我说我正在修改现有应用程序的代码,所以这里的交易我有一个至少有3种形式的页面(数量根据存在的数据而变化),以及他们都调用不同的控制器方法,但我坚持认为,当其中一个表单出现错误时,它会在另一个表单上显示 不良邮政编码和以下代码的更多细节:

所以这是我的整个控制器方法(business.php)

char

}

这是我的观点

class business extends MX_Controller {

public function __construct() {
    parent::__construct();
    $this->auth->check_access('Admin', true);
    $this->load->model("business_model");
    $this->load->model("invoice_model");
    $this->load->model("setting_model");
    $this->load->model("custom_field_model");
    $this->load->library('form_validation');
}

function index() {
    $data['businesses'] = $this->business_model->get_all_businesses();
    $data['fields'] = $this->custom_field_model->get_custom_fields(1);
    $data['page_title'] = lang('businesses');
    $data['body'] = 'business/list';
    $this->load->view('template/main', $data);
}

function add() {
    if ($this->input->server('REQUEST_METHOD') == 'POST') {

        $this->load->helper('string');
        $this->load->library('email');
        $this->load->library('form_validation');

        $users = $this->business_model->get_all_businesses();
        $this->form_validation->set_message('required', lang('custom_required'));
        $this->form_validation->set_message('is_unique', 'This %s already exists.');
        $this->form_validation->set_rules('fname', 'First Name', 'required');
        $this->form_validation->set_rules('lname', "Last Name", 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('contact', "Phone Number", 'required');
        $this->form_validation->set_rules('address', "Address", 'required');

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

            $fname = $this->input->post('fname');
            $lname = $this->input->post('lname');
            $username = strtolower($fname)[0] . strtolower($lname);
            $email = $this->input->post('email');
            $save['name'] = $fname . " " . $lname;
            $save['email'] = $email;
            $save['username'] = $username;
            $password = random_string('alnum', '8');
            $save['password'] = sha1($password);
            $save['contact'] = $this->input->post('contact');
            $save['address'] = $this->input->post('address');
            $save['user_role'] = 1;
            $save['user_type'] = "business";

            $key = $this->business_model->save($save);
            $this->session->set_flashdata('message', lang('doctor_saved'));

            echo 1;
        } else {

            echo '
            <div class="alert alert-danger alert-dismissable">
                                            <i class="fa fa-ban"></i>
                                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-close"></i></button>
                                            <b>Alert!</b>' . validation_errors() . '
                                        </div>
            ';
        }
    }
}

function edit($id = false) {
    if ($this->input->server('REQUEST_METHOD') === 'POST') {
        $this->load->library('form_validation');

        if($this->input->post('submit') == "update")
        {
        $this->form_validation->set_message('required', lang('custom_required'));
        $this->form_validation->set_rules('name', 'lang:name', 'required');
        $this->form_validation->set_rules('email', 'lang:email', 'trim|valid_email|max_length[128]');
        $this->form_validation->set_rules('username', 'lang:username', 'trim|required|');
        $this->form_validation->set_rules('contact', 'lang:phone', 'required');
        if ($this->input->post('password') != '' || $this->input->post('confirm') != '' || !$id) {
            $this->form_validation->set_rules('password', 'lang:password', 'required|min_length[6]');
            $this->form_validation->set_rules('confirm', 'lang:confirm_password', 'required|matches[password]');
        }

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

            $save['name'] = $this->input->post('name');
            $save['email'] = $this->input->post('email');
            $save['username'] = $this->input->post('username');
            $save['contact'] = $this->input->post('contact');
            $save['address'] = $this->input->post('address');
            $save['user_role'] = 1;
            $save['user_type'] = "business";

            if ($this->input->post('password') != '' || !$id) {
                $save['password'] = sha1($this->input->post('password'));
            }

            $this->business_model->update($save, $id);
            $this->session->set_flashdata('message', lang('doctor_updated'));

            echo 1;
        } else {

            echo '
            <div class="alert alert-danger alert-dismissable">
                                            <i class="fa fa-ban"></i>
                                            <button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-close"></i></button>
                                            <b>Alert!</b>' . validation_errors() . '
                                        </div>
            ';
        }
        }
    }
}

                          

        <div class="modal-dialog">
            <div class="modal-content ff">
                <div class="modal-header">

                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="editlabel"><?php echo lang('edit'); ?> <?php echo lang('patient') ?></h4>
                </div>
                <div class="modal-body">

                    <div id="err_edit<?php echo $new->id ?>">  
                        <?php
                        if (validation_errors()) {
                            ?>
                            <div class="alert alert-danger alert-dismissable">
                                <i class="fa fa-ban"></i>
                                <button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="fa fa-close"></i></button>
                                <b><?php echo lang('alert') ?>!</b><?php echo validation_errors(); ?>
                            </div>

                        <?php } ?>  
                    </div>

                    <form method="post">
                        <input type="hidden" name="id" value="<?php echo $new->id; ?>" />
                        <div class="box-body">
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-8">
                                        <label for="name" style="clear:both;"><?php echo lang('name') ?></label>
                                        <input type="text" name="name" value="<?php echo $business->name ?>" class="form-control name">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-8">
                                        <label for="email" style="clear:both;"><?php echo lang('email') ?></label>
                                        <input type="text" name="email" value="<?php echo $business->email ?>" class="form-control email">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-8">
                                        <label for="username" style="clear:both;"><?php echo lang('username') ?></label>
                                        <input type="text" name="username" value="<?php echo $business->username ?>" class="form-control username">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-8">
                                        <label for="password" style="clear:both;"><?php echo lang('password') ?></label>
                                        <input type="password" name="password" value="" class="form-control password">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-8">
                                        <label for="password" style="clear:both;"><?php echo lang('confirm') ?> <?php echo lang('password') ?></label>
                                        <input type="password" name="confirm" value="" class="form-control conifrm">
                                    </div>
                                </div>
                            </div>



                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-8">
                                        <label for="contact" style="clear:both;"><?php echo lang('phone') ?></label>
                                        <input type="text" name="contact" value="<?php echo $business->contact ?>" class="form-control contact">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-8">
                                        <label for="contact" style="clear:both;"><?php echo lang('address') ?></label>
                                        <textarea name="address"  class="form-control address"><?php echo $business->address ?></textarea>
                                    </div>
                                </div>
                            </div>

                            <div class="box-footer">
                                <button type="submit" class="btn btn-primary update" name="update"><?php echo lang('update') ?></button>
                            </div>

                        </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo lang('close') ?></button>  
                </div>
            </div>
        </div>
    </div>
    </form>
    <?php
    $i++;
}
?>

在上面的代码中我删除了一些部分,但只是因为你知道一切正常,除了表格验证的问题,请感谢任何帮助

1 个答案:

答案 0 :(得分:0)

我发现了我的错误,在我正在调用的javacript中

$("#overlay").hide();
$('#err').html(result);

表示两种形式,而该代码将在单一表单错误输出中显示“result”变量