【问题标题】:Codeigniter form validation error shows on another form on the same pageCodeigniter 表单验证错误显示在同一页面上的另一个表单上
【发布时间】:2016-12-09 17:53:25
【问题描述】:

所以在我开始之前我想说我正在修改现有应用程序的代码,所以这是我有一个至少有 3 个表单的页面的交易(数字根据存在的数据而变化),它们都调用不同控制器方法,但我坚持的是,当其中一个表单出现错误时,它会在另一个表单上显示它 生病发布代码和以下代码的更多详细信息:

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

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++;
}
?>

            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="addlabel"><?php echo lang('add'); ?> <?php echo lang('business') ?></h4>
        </div>
        <div class="modal-body">
             <div id="err">  
                <?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" id="add_form" >         
                <!-- rest of code -->
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    $("#add_form").submit(function (event) {
        var form = $(this).closest('form');
        fname = $(form).find('.fname').val();
        lname = $(form).find('.lname').val();
        email = $(form).find('.email').val();
        contact = $(form).find('.contact').val();
        address = $(form).find('.address').val();
        //alert(blood_id);return false;

        call_loader_ajax();
        $.ajax({
            url: '<?php echo site_url('admin/business/add') ?>',
            type: 'POST',
            data: {fname: fname, lname: lname, email: email, contact: contact, address: address},

            success: function (result) {
                //alert(result);return false;
                if (result == 1)
                {
                    alert('The account has been succesfully create \n An email has been sent to notify the user');
                    location.reload();
                } else
                {
                    $("#overlay").hide();
                    $('#err').html(result);
                }

            }
        });

        event.preventDefault();
    });

    $(".update").click(function (event) {
        event.preventDefault();
        //$(this).closest("form").submit(); 
        var form = $(this).closest('form');
        id = $(form).find('input[name=id]').val();
        name = $(form).find('input[name=name]').val();
        username = $(form).find('input[name=username]').val();
        email = $(form).find('input[name=email]').val();
        password = $(form).find('input[name=password]').val();
        conf = $(form).find('input[name=confirm]').val();
        contact = $(form).find('input[name=contact]').val();
        address = $(form).find('.address').val();
        //alert(blood_id);return false;
        call_loader_ajax();
        $.ajax({
            url: '<?php echo site_url('admin/business/edit') ?>/' + id,
            type: 'POST',
            data: {name: name, username: username, email: email, password: password, confirm: conf, contact: contact, address: address},

            success: function (result) {
                //alert(result);return false;
                if (result == 1)
                {
                    location.reload();
                } else
                {
                    $("#overlay").hide();
                    $('#err').html(result);
                }

            }
        });


    });


    $(function () {
        $('#example1').dataTable({
        });
    });

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

【问题讨论】:

    标签: javascript php jquery forms codeigniter


    【解决方案1】:

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

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

    对于这两种形式,而该代码将在单个形式错误输出中显示“结果”变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多