Welcome Guest, Not a member yet? Register   Sign In
form_validation->run() always return false
#1

tanzeem Wrote:I have the following method in my controller
PHP Code:
public function add_employee()
    {
        $employee $this->Employee_model;

        $data['employee'] = $this->Employee_model->getAll();
        $data['designation'] = $this->Employee_model->getAllDesignations();
        $data['department'] = $this->Department_model->getAll();

        $this->form_validation->set_rules('int_CDIT_employeeID''C-DIT Employee ID''required|alpha_numeric', [
            'required' => 'Employee ID is required!',
            'alpha_numeric' => 'Employee ID should contain only numbers and alphabets!'
        ]);

        $this->form_validation->set_rules('vchr_employeeName''Employee Name''required', [
            'required' => 'Employee Name is required!'
        ]);

        $this->form_validation->set_rules('vchr_email_id''Email''required|valid_email', [
            'required' => 'Email cannot be empty!',
            'valid_email' => 'Invalid email!'
        ]);

        $this->form_validation->set_rules('int_mobileNo''Mobile Number''required|min_length[10]|max_length[10]|numeric', [
            'required' => 'Fill in the employee\'s mobile number!',
            'min_length' => 'Invalid Mobile number!',
            'max_length' => 'Invalid Mobile number!',
            'numeric'    => 'Mobile number should contain only numbers'
        ]);

        $this->form_validation->set_rules('int_designationID''Designation''required', [
            'required' => 'Designation is required!'
        ]);

        $this->form_validation->set_rules('int_departmentID''Department''required', [
            'required' => 'Department is required!'
        ]);

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

            $data['title'] = 'Add Employee';
            $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();

            $data $this->security->xss_clean($data);

            $this->load->view('templates/admin_header'$data);
            $this->load->view('templates/admin_sidebar');
            $this->load->view('templates/admin_topbar'$data);
            $this->load->view('employee/add_employee',$data);//pass division information to be displayed under select
            $this->load->view('templates/admin_footer');

        } else {
            $employee->save();
            $this->session->set_flashdata('employeemessage''<div class="alert alert-success" role="alert">
            Employee added successfully!</div>'
);
            redirect('employee');
        }
    

The employee model has the following content:
PHP Code:
class Employee_model extends CI_Model {

    public function getAll()
    {
      $this->db->select('tbl_employee.id,tbl_employee.int_CDIT_employeeID,
      tbl_employee.vchr_employeeName,tbl_employee.vchr_email_id,
      tbl_employee.int_mobileNo,tbl_department.vchr_departmentName,
      tbl_employee.int_departmentID, tbl_employee.int_designationID,
      tbl_designation.vchr_designation '
);

      $this->db->from('tbl_employee');
      $this->db->join('tbl_department','tbl_employee.int_departmentID = tbl_department.id');
      $this->db->join('tbl_designation','tbl_designation.id = tbl_employee.int_designationID');
      $query $this->db->get();

      return($query->result_array());

    }
    public function getAllDesignations(){
      return $this->db->get('tbl_designation')->result_array();

    }

    public function getById($id)
    {
        return $this->db->get_where('tbl_employee', ['id' => $id])->row_array();
    }

    public function save()
    {
        $post $this->input->post();
        //$this->id              = $this->security->xss_clean(uniqid());
        $this->int_CDIT_employeeID    $this->security->xss_clean($post['int_CDIT_employeeID']);
        $this->vchr_employeeName    $this->security->xss_clean($post['vchr_employeeName']);
        $this->vchr_email_id          $this->security->xss_clean($post['vchr_email_id']);
        $this->int_mobileNo          $this->security->xss_clean($post['int_mobileNo']);
        $this->int_designationID          $this->security->xss_clean($post['int_designationID']);
        $this->int_departmentID          $this->security->xss_clean($post['int_departmentID']);

        return $this->db->insert('tbl_employee'$this);
    }

    public function update()
    {
      $post $this->input->post();
      $this->id $this->security->xss_clean($post['id']);
      $this->int_CDIT_employeeID    $this->security->xss_clean($post['int_CDIT_employeeID']);
      $this->vchr_employeeName    $this->security->xss_clean($post['vchr_employeeName']);
      $this->vchr_email_id          $this->security->xss_clean($post['vchr_email_id']);
      $this->int_mobileNo          $this->security->xss_clean($post['int_mobileNo']);
      $this->int_designationID          $this->security->xss_clean($post['int_designationID']);
      $this->int_departmentID          $this->security->xss_clean($post['int_designationID']);

      $this->db->update('tbl_employee'$this, ['id' => $this->id]);

    }

    public function delete($id)
    {
        return $this->db->delete('tbl_employee', ['id' => $id]);
    }



My view employee/add_employee.php has the following content
Code:
<!-- Begin Page Content -->
<div class="container-fluid">

    <!-- Page Heading -->
    <h1 class="h3 mb-4 text-gray-800">Add Employees </h1>

    <div class="card shadow mb-3">
        <div class="card-header">
            Add Employees
        </div>

        <div class="card-body">
          <?php echo form_open('employee/add_employee');?>

                <div class="form-group">
                    <label for="int_CDIT_employeeID">CDIT Employee ID*</label>
                    <input class="form-control"
                    type="text" name="int_CDIT_employeeID" placeholder="Enter CDIT Employee ID" value="<?= set_value('CDIT Employee ID'); ?>">
                    <?= form_error('int_CDIT_employeeID', '<small class="text-danger pl-3">', '</small>'); ?>
                </div>

                <div class="form-group">
                    <label for="vchr_employeeName">Employee Name*</label>
                    <input class="form-control"
                    type="text" name="vchr_employeeName" placeholder="Enter Employee Name" value="<?= set_value('vchr_employeeName'); ?>">
                    <?= form_error('vchr_employeeName', '<small class="text-danger pl-3">', '</small>'); ?>
                </div>

                <div class="form-group">
                    <label for="vchr_email_id">E-Mail ID*</label>
                    <input class="form-control"
                    type="text" name="vchr_email_id" placeholder="Enter Email ID" value="<?= set_value('vchr_email_id'); ?>">
                    <?= form_error('vchr_email_id', '<small class="text-danger pl-3">', '</small>'); ?>
                </div>

                <div class="form-group">
                    <label for="int_mobileNo">Mobile Number*</label>
                    <input class="form-control"
                    type="number" minlength="10" maxlength="10" name="int_mobileNo" placeholder="Enter Mobile Number" value="<?= set_value('int_mobileNo'); ?>">
                    <?= form_error('int_mobileNo', '<small class="text-danger pl-3">', '</small>'); ?>
                </div>

                <div class="form-group">
                    <label for="vchr_designation">Enter Designation *</label>
                    <select name="vchr_designation" id="vchr_designation" class="form-control">
                        <option value="">---Select Designation---</option>
                        <?php foreach ($designation as $d) : ?>
                        <option value="<?= $d['id']; ?>"><?= $d['vchr_designation']; ?></option>
                        <?php endforeach; ?>
                    </select>
                    <?= form_error('vchr_designation', '<small class="text-danger pl-3">', '</small>'); ?>

                </div>
                <div class="form-group">
                    <label for="vchr_departmentName">Enter Department</label>
                    <select name="vchr_departmentName" id="vchr_departmentName" class="form-control">
                        <option value="">---Select Department---</option>
                        <?php foreach ($department as $d) : ?>
                        <option value="<?= $d['id']; ?>"><?= $d['vchr_departmentName']; ?></option>
                        <?php endforeach; ?>
                    </select>
                    <?= form_error('vchr_departmentName', '<small class="text-danger pl-3">', '</small>'); ?>

                </div>


                <!-- button save -->
                <input class="btn btn-success" type="submit" name="btn" value="Add Employee!" />
            </form>
        </div>

        <div class="card-footer small text-muted">
            * must be filled
        </div>
</div>

</div>
<!-- /.container-fluid -->

</div>
<!-- End of Main Content -->

</div>
<!-- /.container-fluid -->

</div>
<!-- End of Main Content -->

Whenever I submit the form with the Add Employee button, the 

PHP Code:
$this->form_validation->run()  
always return false even though the fields are filled properly.
How can I fix this problem
Reply
#2

What version of CodeIgniter are you using?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

[quote pid="398015" dateline="1656922313"]
Quote:I used codeigniter3. I found the solution, I had name mismatch for  input data in the view and corresponding variables in controller when fetching data. Found it and fixed. Thanks

[/quote]
Reply
#4

Exactly
The same thing happened and I fixed it too
Reply




Theme © iAndrew 2016 - Forum software by © MyBB