rahul
rahul

Reputation: 418

How to get validation errors in codeigniter?

Hello I am inserting data in database. When I insert both category and description then data in inserting but when I don't insert in the category and description input and click on create then no error showing with blank page admin/category/ register_category, I want to show that category and description field should not be empty.

category.php view page is below :

<?php if(isset($_SESSION['success'])){ ?>
    <div class="alert alert-success"><?php echo $_SESSION['success']; ?> 
    </div>
<?php } ?>
<?php echo validation_errors('<div class="alert alert-danger">','</div>'); ?>

<form class="form" action="<?php echo site_url('admin/category/register_category') ?>" method="POST">
    <label for="contactinput5">Category Name</label>
    <input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
    <label for="contactinput5">Discription</label>
    <textarea class="form-control border-primary" type="text" placeholder="discription" name="discription" id="contactemail5"></textarea>
    <button type="submit" name="create" class="btn btn-primary">

and my controller Category.php page is:

<?php
class Category extends CI_Controller {
    function index() {
        $this->load->view('admin/category');
    }

    function register_category() {      
        $this->form_validation->set_rules('category', 'Category', 'required');
        $this->form_validation->set_rules('discription', 'Discription', 'required');
        if($this->form_validation->run() == TRUE){
            echo "form validate";
            $this->load->model('categories');
            $insert_category = $this->categories->validate();
            if($insert_category){
                $this->session->set_flashdata("success","Your data has been added");
                redirect("admin/category","refresh");
            }
            else{
                redirect('admin/category');
            }
        }
    }
}
?>  

model categories page:

<?php
class Categories extends CI_Model
{
     function validate()
    {
        $arr['categoryname'] =  $this->input->post('category');
        $arr['discription'] =   $this->input->post('discription');
        return $this->db->insert('category',$arr);      
    }
}
?>

Upvotes: 0

Views: 7337

Answers (5)

Alex
Alex

Reputation: 9265

FYI your solution only worked because validation_errors() only applies to the current instance. When you redirect that information is lost. You would have to store it in session variable or this is common (change form action to self or leave blank):

function index() {
        if ($_POST) {
            $this->form_validation->set_rules('category', 'Category', 'required');
            $this->form_validation->set_rules('discription', 'Discription', 'required');
            if($this->form_validation->run() == TRUE){
                $this->load->model('categories');
                $insert_category = $this->categories->validate();
                if($insert_category){
                    $this->session->set_flashdata("success","Your data has been added");
                }
            }
        }
        $this->load->view('admin/category');
    }

Of course your way works too if you are ok with the url changing.

Upvotes: 0

Praveen Kumar
Praveen Kumar

Reputation: 927

Hello Please update your function in the controller. There is issue in validation condition Changes TURE to FALSE. Check below code.

function register_category()
    {       
        $this->form_validation->set_rules('category', 'Category', 'required');
        $this->form_validation->set_rules('discription', 'Discription', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('category');
        }
        else
        {
            $this->load->model('categories');
            $insert_category = $this->categories->validate();

            if($insert_category)
            {
                $this->session->set_flashdata("success","Your data has been added");
                redirect("admin/category","refresh");
            }
            else
            {
                redirect('admin/category');
            }
        }
    }

Upvotes: 1

rahul
rahul

Reputation: 418

hey guys thanks and i got my answer just by putting this code

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

Upvotes: 2

AngularJMK
AngularJMK

Reputation: 1268

Get all post validation errors in controller :
echo validation_errors();

If you want to show at the end of textbox use this following method :

<label for="contactinput5">Category Name</label>
<input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
<?php echo form_error('category', '<div class="error">', '</div>'); ?>

Upvotes: 0

Van Tho
Van Tho

Reputation: 633

If validation result is not true, you can get errors from $this->form_validation->error_array(), loop the return array and show the error to the user.

Hope this help.

Upvotes: 2

Related Questions