1

I'm using PHP Code Igniter and am having trouble with a form validation. I followed the form validation tutorial on Code Igniter but it is not working. Empty fields are submitted to the database.

I added the form validation rule sets to the index function in the controller.

Controller:

   <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Phonebook extends CI_Controller {

  var $TPL;

  public function __construct()
  {
    parent::__construct(); 

    $this->TPL['newentry'] = false;
    $this->load->library(array('form_validation')); // load form lidation libaray & session library
    $this->load->helper(array('url','form'));  // load url,html,form helpers optional
  }

   public function index()
  {     
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    $TPL->form_validation->set_rules('fname', 'First Name', 'required|min_length[5]|max_length[12]|is_unique[phonebook.fname]');
    $this->form_validation->set_rules('lname', 'Last Name', 'required|matches[passconf]');
    $this->form_validation->set_rules('phone', 'Password Confirmation', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[phonebook.email]');

    if ($this->form_validation->run() == FALSE)
    {
      $this->load->view('phonebook_view');
    }
    else
    {
      $this->load->view('success_message');
    }
  }

  public function newentry()
  { 
    $fname = $this->input->post("fname");
    $lname = $this->input->post("lname");
    $phone = $this->input->post("phone");
    $email = $this->input->post("email");
    $query = $this->db->query("INSERT INTO phonebook VALUES (NULL, '$fname', '$lname', '$phone', '$email', NULL, NULL, NULL, NULL);");

    $this->display(); 
  }

  public function addnew()
  {
    $this->TPL['newentry'] = TRUE;

    $this->display();
  }

}

And the View file.

<html>
<body>
<h1><a href="<?= base_url()?>">Phonebook</a></h1>

<? if ($newentry) { ?>

<?= form_open('Phonebook/newentry') ?>
<?= form_fieldset("Add Entry") ?>
<?= form_label('First Name:', 'fname'); ?> <br>
<?= form_input(array('name' => 'fname',
 'id' => 'fname')); ?> <br>
<?= form_label('Last Name:', 'lname'); ?> <br>
<?= form_input(array('name' => 'lname', 
 'id' => 'lname')); ?> <br>
<?= form_label('Phone Number:', 'phone'); ?> <br>
<?= form_input(array('name' => 'phone',
 'id' => 'phone')); ?> <br>
<?= form_label('E-mail:', 'email'); ?> <br>
<?= form_input(array('name' => 'email',
 'id' => 'email')); ?> <br>
<?= form_submit('phonebooksubmit', 'Submit'); ?>
<?= form_fieldset_close(); ?>
<?= form_close() ?>

<? } else { ?>

<p><a href="<?= base_url() ?>index.php?/Phonebook/addnew">Add new entry</a></p>

<? } ?>

</body>
</html>

1 Answer 1

3

If you compare your code here and basic example in CI userguide, you can see those two are not similar examples. You missed to load session library in array passed in constructor. Feel free to remove loading libraries and helpers from other methods (i.e. index) because those are loaded in constructor already and respective objects/functions are available in all class methods after. Make array of public variable, var is deprecated keyword.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Phonebook extends CI_Controller
{

    public $TPL = array();

    public function __construct()
    {
        parent::__construct(); 

        $this->TPL['newentry'] = false;
        $this->load->library(array('form_validation, session')); // load form lidation libaray & session library
        $this->load->helper(array('url', 'html', 'form'));  // load url,html,form helpers optional
    }

    public function index()
    {
        $this->form_validation->set_rules('fname', 'First Name', 'required|min_length[5]|max_length[12]|is_unique[phonebook.fname]');
        $this->form_validation->set_rules('lname', 'Last Name', 'required|matches[passconf]');
        $this->form_validation->set_rules('phone', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[phonebook.email]');

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('phonebook_view');
    }
    else
    {
        $fname = $this->input->post("fname");
        $lname = $this->input->post("lname");
        $phone = $this->input->post("phone");
        $email = $this->input->post("email");
        $query = $this->db->query("INSERT INTO phonebook VALUES (NULL, '$fname', '$lname', '$phone', '$email', NULL, NULL, NULL, NULL);");

        if ((int)$this->db->affected_rows() < 1)
        {
            print_r($this->db->error());
            exit;
        }
        else
        {
            redirect('success_page', 'refresh');
            //maybe? $this->display();
        }
    }

    public function addnew()
    {
        $this->TPL['newentry'] = TRUE;

        $this->display();
    }
}

Put <?= form_open('phonebook') ?> in form. Also, fix set_rules(). I just copied/pasted yours but not seem right. Read name of fierlds and coresponding rules again in this code - doesn't match.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.