简体   繁体   中英

Codeigniter validation errors are not displaying

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM