简体   繁体   中英

Codeigniter form validation error shows on another form on the same page

So before i start i wana say i am modifying the code of an existing app, So here's the deal i have a page that has at least 3 form(the number varies based on the data present), and they all call different controller method, but where im stuck at is that when there is an error on one of the forms, it displays it on another form Ill post code and more details on code below:

So here's my entire controller method (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>
            ';
        }
        }
    }
}

}

and here is my view

        <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({
        });
    });

In the above code i cut some parts off of it, but just so you know everything is working fine,except for the issue of form validation, please any help is appreciated thanks

I found my error, in my javacript i was calling

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

for the two forms, whereas that code will display the "result" variable in a single form error output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM