0

My codeigniter validation errors are not displaying someone can help? my code is

public function addProduct(){
    $this->load->view('header', $this->data);
    $this->load->view('product/addProduct');
    $this->load->view('footer');

        $this->form_validation->set_rules('productName', 'Product Name', 'required|trim');
        $this->form_validation->set_rules('productPrice', 'Product Price', 'required|trim');
        if (!$this->form_validation->run() == FALSE)
        {
            // some stuff on validation success
        }
        else{
            $this->load->view('product/addProduct');
        }

}

and i have added echo validation_errors(); in my view and action of the form is product/addProduct.

4
  • Because of you have to pass validation error in your function Commented Aug 5, 2016 at 7:42
  • @Nikhil Vaghla i have to pass validation error in another function ? Commented Aug 5, 2016 at 7:43
  • remove the first one $this->load->view('product/addProduct'); Commented Aug 5, 2016 at 10:52
  • use this way if ($this->form_validation->run() == FALSE) { $this->load->view('product/addProduct'); } else{ // some stuff on validation success } Commented Aug 5, 2016 at 10:55

2 Answers 2

1

Try this it's work for you.

form_error() function return your form error.

        $post_fields = $this->input->post();
        $data['msg'] = '<ul>';
        foreach ($post_fields as $k => $v) {
            if (form_error($k))
                $data['msg'] .= "<li>" . strip_tags(form_error($k)) . "</li>\n";
        }
        $data['msg'].='</ul>';
        $this->load->view('product/addProduct',$data);

OR

echo validation_errors();//this function also return form  error.
Sign up to request clarification or add additional context in comments.

3 Comments

Thank You . but could you please explain the faults in my code.
In your code you can not pass error in else part. You need to post your error in your view
if you have happy with answer then accept answer and upvote.
0

On view example

<?php echo validation_errors('<div class="error">', '</div>'); ?>

<!-- lower case for the controller name on form open -->

<?php echo form_open_multipart('product/addProduct');?>

<h5>productName</h5>
<input type="text" name="productName" value="<?php echo set_value('productName'); ?>" size="50" />

<h5>productPrice</h5>
<input type="text" name="productPrice" value="<?php echo set_value('productPrice'); ?>" size="50" />

<div><input type="submit" value="Submit" /></div> 

<?php echo form_close();?>

Controller

Make sure your file name and class name is something like this below where first letter only upper case

Guide

Filename: Product.php

<?php

class Product extends CI_Controller {

 public function __construct() {
    parent::__construct();
    $this->load->library('form_validation');
    $this->load->helper('form');
    $this->load->helper('url');
 }

 public function addProduct(){

    // You can get data from here also

    $this->data['some'] = 'Blah';

    $this->form_validation->set_rules('productName', 'Product Name', 'required|trim');
    $this->form_validation->set_rules('productPrice', 'Product Price', 'required|trim');

    // remove your !

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

        // You can just display view in this area you do not have to load it multiple times

        $this->load->view('header', $this->data);
        $this->load->view('product/addProduct');
        $this->load->view('footer');

    } else {

        // some stuff on validation success
    }

 }

}

Also check you have set your base url in config.php is required now in CI3 versions.

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.