How can I display each error next to input field, or only one error for all fields (like "all fields are required") down below? I prefer each error next to input.
-
View: I'm using form_open and later form_input. form_eror doesn't work.
// INPUT FORM FOR PRODUCT INSERT // Form open echo form_open('insert_product',array('id' =>'form')); //all form inputs // Name echo form_label('Name ','name'); $name = array( 'name' => 'productName', 'id' => 'productName', 'placeholder' => 'Enter product name', ); echo form_input($name, set_value('name')); echo form_error('name');
-
Controller validation rules in array:
// Define validation rules for form inputs $validation_rules = array( //Name array( 'field' => 'productName', 'label' => 'Product name', 'rules' => 'required|min_length[3]' ),
-
Validation check in controller :
//Check validation if ( $this->form_validation->run() == true ) { //Preparing input data, keys of array are the same as fields in database table $data = array( 'Name' => $this->input->post('productName'), 'Description' => $this->input->post('productDescription'), 'Production_date' => date('Y-m-d', strtotime( $this->input->post('productProduction_date' ))), 'Expiry_date' => date('Y-m-d', strtotime( $this->input->post('productExpiry_date' ))), 'Price' => $this->input->post('productPrice'), 'Currency' => $this->input->post('productCurrency'), 'EAN_code' => $this->input->post('productEAN_code'), 'Weight' => $this->input->post('productWeight'), 'Weight_unit' => $this->input->post('productWeight_unit') ); //Transfer data to model and get message from model $row = $this->ProductModel->insert_product($data); //get answer if ($row) { echo "Product successfully added to database"; }else { echo 'There was an error inserting data in database '.json_encode($row); } } else { echo validation_errors(); }