how to use both form validation as well user id exists in codeIgniter

user3840898

i use the following code for form validation and employee code already exists.i can easily validate the form but i am unable to display the message for if employee code already exist

as the following code for the controllers/employee.php

public function add_employee($site_id = false)
{

$this->load->helper(array('form', 'url'));

$this->load->library('form_validation');

$this->form_validation->set_rules('name', 'Name', 'trim|required|max_length[50]|xss_clean');
$this->form_validation->set_rules('father_name', 'Father Name', 'trim|required|max_length[50]|xss_clean');
$this->form_validation->set_rules('DOB', 'Date Of Birth', 'trim|required|max_length[50]|xss_clean');
$this->form_validation->set_rules('mobile_no', 'Mobile Number', 'trim|required|min_length[10]|max_length[10]|integer|xss_clean');
$this->form_validation->set_rules('email_id', 'Email Id', 'valid_email|xss_clean');
$this->form_validation->set_rules('e_code', 'Employee Code', 'trim|required|max_length[20]|xss_clean');

//$this->form_validation->set_rules('local_address', 'Local Address', 'trim|required|max_length[200]|xss_clean');
$this->form_validation->set_rules('permanent_address', 'Permanent Address', 'trim|required|max_length[200]|xss_clean');
//$this->form_validation->set_rules('adhaar_no', 'Adhaar Number', 'trim|required|min_length[12]|max_length[12]|xss_clean');
//$this->form_validation->set_rules('account_no', 'Account Number', 'trim|required|max_length[50]|xss_clean');

//$this->form_validation->set_rules('bank_name', 'Bank Name', 'trim|required|max_length[50]|xss_clean');
//$this->form_validation->set_rules('branch_name', 'Branch Name', 'trim|required|max_length[50]|xss_clean');
//$this->form_validation->set_rules('ifsc_code', 'IFSC Code', 'trim|required|max_length[50]|xss_clean');

$this->form_validation->set_rules('reference_by', 'Reference By', 'trim|required|max_length[50]|xss_clean');
$this->form_validation->set_rules('work_location', 'Work Location', 'trim|required|xss_clean');
$this->form_validation->set_rules('designation', 'Designation', 'trim|required|max_length[50]|xss_clean');
$this->form_validation->set_rules('salary', 'Salary', 'trim|required|max_length[50]|xss_clean');
$this->form_validation->set_rules('joining_date', 'Joining Date', 'trim|required|max_length[50]|xss_clean');
$this->form_validation->set_rules('pan_no', 'Pan Number', 'trim|regex_match[/^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$/]|max_length[50]|xss_clean');
//$this->form_validation->set_rules('id_proof', 'ID Proof', 'trim|required|max_length[50]|xss_clean');
//$this->form_validation->set_rules('status', 'Status', 'trim|required|xss_clean');

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

employee::add_employee_form();
}
else
{

$count=$this->employee_model->add_employee($_POST);

if($count>0)
{
employee::add_employee_form();  
}
else{
if(!$site_id) {
redirect(base_url().'employee'); } else {
redirect(base_url().'site_ho_view/'.$site_id);
}
}
}
}

and for the view/add_employee_form as below

<div class="row">
    <div class="col-sm-8">
    <div class="box-register">

        <div class="panel panel-default">



            <div class="panel-heading">
                <i class="fa fa-external-link-square"></i>
                Employee details
            </div>


            <?php
            $attributes = array('class' => 'form-horizontal');
            echo form_open_multipart('employee/add_employee', $attributes);?>
            <div class="panel-body">



                <?php echo form_error('name'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Name
                                            </label>
                                            <div class="col-sm-8">
                <input type="hidden" name="confirmed" value="0">
                                    <input type="text" class="form-control" name="name" placeholder="Name" value="<?php echo set_value('name'); ?>">
                </div>
                </div>




                <?php echo form_error('e_code'); ?>
                            <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Employee Code
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="e_code" placeholder="Employee Code" value="<?php echo set_value('e_code'); ?>">
                </div>
                </div>

                <?php echo form_error('father_name'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Father Name
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="father_name" placeholder="Father Name" value="<?php echo set_value('father_name'); ?>">
                </div>
                </div>

                <?php echo form_error('DOB'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Date Of Birth
                                            </label>
                                            <div class="col-sm-8">
                          <input type="text" class="form-control" id="datepicker" name="DOB" placeholder="Date Of Birth" value="<?php echo set_value('DOB'); ?>" >


                </div>
                </div>

                <?php echo form_error('mobile_no'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Mobile No.
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="mobile_no" placeholder="Mobile Number" value="<?php echo set_value('mobile_no'); ?>">
                </div>
                </div>

                <?php echo form_error('email_id'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Email ID
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="email_id" placeholder="Email ID" value="<?php echo set_value('email_id'); ?>">
                </div>
                </div>


                <?php echo form_error('local_address'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Local Address
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="local_address" placeholder="Local Address" value="<?php echo set_value('local_address'); ?>">
                </div>
                </div>

                <?php echo form_error('permanent_address'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Permanent Address
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="permanent_address" placeholder="Permanent Address" value="<?php echo set_value('permanent_address'); ?>">
                </div>
                </div>

                <?php echo form_error('adhaar_no'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Adhaar No.
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="adhaar_no" placeholder="Adhaar No" value="<?php echo set_value('adhaar_no'); ?>">
                </div>
                </div>



                <?php echo form_error('bank_name'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Bank Name
                                            </label>
                                            <div class="col-sm-8">
                                            <input type="text" class="form-control" name="bank_name" placeholder="Bank Name" value="<?php echo set_value('bank_name'); ?>">
                </div>
                </div>

                <?php echo form_error('account_no'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Account No.
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="account_no" placeholder="Account No" value="<?php echo set_value('account_no'); ?>">
                </div>
                </div>

                <?php echo form_error('branch_name'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Branch Name
                                            </label>
                                            <div class="col-sm-8">

                                    <input type="text" class="form-control" name="branch_name" placeholder="Branch Name" value="<?php echo set_value('branch_name'); ?>">
                </div>
                </div>

                <?php echo form_error('ifsc_code'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                IFSC Code
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="ifsc_code" placeholder="IFSC Code" value="<?php echo set_value('ifsc_code'); ?>">
                </div>
                </div>

                <?php echo form_error('reference_by'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Reference By
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="reference_by" placeholder="Reference By" value="<?php echo set_value('reference_by'); ?>">
                </div>
                </div>

                <?php echo form_error('work_location'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Work Location
                                            </label>
                                            <div class="col-sm-8">
                <select id="form-field-select-1" class="form-control search-select" name='work_location' style="border:1px;">
                                            <option value="">Select Work Location</option>

                                            <?php foreach($all_construction_sites as $site)
                                            { ?>
                                            <option value="<?php echo $site['id']; ?>"



                                            <?php if(set_value('work_location') == $site['id']) { ?> selected <?php } ?>


                                            ><?php echo $site['name']; ?></option>
                                            <?php } ?>




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

                <?php echo form_error('designation'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Designation
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="designation" placeholder="Designation" value="<?php echo set_value('designation'); ?>">
                </div>
                </div>

                <?php echo form_error('salary'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Salary
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="salary" placeholder="Salary" value="<?php echo set_value('salary'); ?>">
                </div>
                </div>

                <?php echo form_error('joining_date'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Joining Date
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="joining_date" id="datepicker2" placeholder="Joining Date" value="<?php echo set_value('joining_date'); ?>" readonly>
                </div>
                </div>

                <?php echo form_error('pan_no'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                PAN No
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="pan_no" placeholder="Pan Number" value="<?php echo set_value('pan_no'); ?>">
                </div>
                </div>

                <?php echo form_error('id_proof'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                ID Proof
                                            </label>
                                            <div class="col-sm-8">
                                    <input type="text" class="form-control" name="id_proof" placeholder="ID Proof" value="<?php echo set_value('id_proof'); ?>">
                </div>
                </div>

                <?php echo form_error('status'); ?>
                <div class="form-group">
                <label class="col-sm-3 control-label" for="form-field-3" >
                                                Status
                                            </label>
                                            <div class="col-sm-8">
                <select id="form-field-select-1" name="status" class="form-control search-select" style="border:1px;">
                                            <option value="">Select Status</option>
                                            <option value="working"


                                            <?php if(set_value('status') == 'working') { ?> selected <?php } ?>


                                            >Working</option>
                                            <option value="on_leave"

                                            <?php if(set_value('status') == 'on_leave') { ?> selected <?php } ?>

                                            >On Leave</option>
                                            <option value="terminated"

                                            <?php if(set_value('status') == 'terminated') { ?> selected <?php } ?>

                                            >Terminated</option>
                                            <option value="retired"

                                            <?php if(set_value('status') == 'retired') { ?> selected <?php } ?>

                                            >Retired</option>



                                        </select>


                </div>
                </div>




                            </div>
        </div>

and the empmployee_model.php/add_employee as follow

function add_employee($data)
    {   
        $query = $this->db->get_where('spar_employee', array('e_code' => $data['e_code']
        ));

        $count = $query->num_rows();

       if($count>0)
       {


           return $count;

       }
       else{
        $data['created'] = date("Y-m-d");
        $data['last_user'] = $this->session->userdata('user_id');
        $this->db->insert('spar_employee', $data);
       }

    }

plz help me

Timothy Zorn

You can set a rule to check if the value would be unique in a table.field. This is what I would do:

$this->form_validation->set_rules('e_code', 'Employee Code', 'trim|required|max_length[20]|xss_clean|is_unique[spar_employee.e_code]');

source: https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#rulereference

Edit: as a note, you can also set a custom error message with the following:

$this->form_validation->set_message('is_unique', 'The value you provided in the %s field is not unique. It already exists for another employee.');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use post() jquery method with codeigniter form_validation

From Dev

How to use both Disjunction and Validation?

From Dev

Use language files in form_validation of Codeigniter

From Dev

How To Bootstrap Integrate with Codeigniter Form Validation

From Dev

How to set default values for CodeIgniter Form Validation?

From Dev

Django Form use of user id

From Dev

How to use both internal Forms authenticationas well as Azure AD authentication

From Dev

How use apache kafka in vertx, both on the server side, as well as the client?

From Dev

codeigniter how to use the alternative database connection in form validation using unique rule

From Dev

How do I use CodeIgniter's `set_value()` function when using `form_validation.php`?

From Dev

how to use order by field as well sort by id

From Dev

Should i use both backend and frontend form validation?

From Dev

form validation not working when use ajax id

From Dev

codeigniter : form validation jquery

From Dev

form_validation CodeIgniter

From Dev

Use a callback function on form validation rules from an external library - Codeigniter

From Dev

Use both Telerik RadGrid's OnRowClick as well as custom User Control Event Handler

From Dev

How to check if user with a specific ID exists?

From Dev

How to set form_validation to required for Files in Codeigniter?

From Dev

How to set private class to form validation callback in codeigniter

From Dev

How to change the font of default CodeIgniter form validation Error

From Dev

How to return data from a form validation callback method in CodeIgniter?

From Dev

can i use both jdbcTemplet as well as hibernate

From Dev

how to use multi level user login in codeigniter ?

From Dev

Codeigniter form validation required array

From Dev

Codeigniter form validation multiple callbacks

From Dev

codeigniter form validation rules url

From Dev

Bootstrap Modal With Codeigniter Form Validation

From Dev

Codeigniter form validation is not showing errors

Related Related

  1. 1

    How to use post() jquery method with codeigniter form_validation

  2. 2

    How to use both Disjunction and Validation?

  3. 3

    Use language files in form_validation of Codeigniter

  4. 4

    How To Bootstrap Integrate with Codeigniter Form Validation

  5. 5

    How to set default values for CodeIgniter Form Validation?

  6. 6

    Django Form use of user id

  7. 7

    How to use both internal Forms authenticationas well as Azure AD authentication

  8. 8

    How use apache kafka in vertx, both on the server side, as well as the client?

  9. 9

    codeigniter how to use the alternative database connection in form validation using unique rule

  10. 10

    How do I use CodeIgniter's `set_value()` function when using `form_validation.php`?

  11. 11

    how to use order by field as well sort by id

  12. 12

    Should i use both backend and frontend form validation?

  13. 13

    form validation not working when use ajax id

  14. 14

    codeigniter : form validation jquery

  15. 15

    form_validation CodeIgniter

  16. 16

    Use a callback function on form validation rules from an external library - Codeigniter

  17. 17

    Use both Telerik RadGrid's OnRowClick as well as custom User Control Event Handler

  18. 18

    How to check if user with a specific ID exists?

  19. 19

    How to set form_validation to required for Files in Codeigniter?

  20. 20

    How to set private class to form validation callback in codeigniter

  21. 21

    How to change the font of default CodeIgniter form validation Error

  22. 22

    How to return data from a form validation callback method in CodeIgniter?

  23. 23

    can i use both jdbcTemplet as well as hibernate

  24. 24

    how to use multi level user login in codeigniter ?

  25. 25

    Codeigniter form validation required array

  26. 26

    Codeigniter form validation multiple callbacks

  27. 27

    codeigniter form validation rules url

  28. 28

    Bootstrap Modal With Codeigniter Form Validation

  29. 29

    Codeigniter form validation is not showing errors

HotTag

Archive