Helo,
So I have a little problem with form_validation in Codeigniter, here is the controller -
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Records extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->view('includes/header');
$this->load->view('products');
$this->load->view('includes/footer');
}
public function addRecord() {
$this->load->helper(array('form', 'url'));
$this->load->helper('url');
$query = $this->db->get_where('users', array('uname' => $this->session->userdata('username')));
foreach($query->result_array() as $row) {
$data = array(
'username' => $row['uname'],
'email' => $row['email'],
'name' => $row['name'],
'description' => $row['description'],
'picture' => $row['picture'],
'gender' => $row['gender'],
);
}
if($this->session->userdata('logged_in')) {
$this->load->view('includes/header');
$this->load->view('my_records', $data);
$this->load->view('includes/footer');
}
else {
redirect('/home', 'refresh');
}
}
public function postRecord() {
$this->lang->load('form_validation', 'latvian');
$this->load->library('form_validation');
$this->load->model('records');
$this->load->helper(array('form', 'url'));
if(!$this->session->userdata('logged_in')) redirect('/home', 'refresh');
$this->form_validation->set_rules('message', 'Ziņa', 'required|trim|min_length[5]|max_length[250]');
$query = $this->db->get_where('users', array('uname' => $this->session->userdata('username')));
foreach($query->result_array() as $row) {
$data = array(
'username' => $row['uname'],
'email' => $row['email'],
'name' => $row['name'],
'description' => $row['description'],
'picture' => $row['picture'],
'gender' => $row['gender'],
);
}
// If doesn't pass validation, redirect's back with errors.
if ($this->form_validation->run() == FALSE)
{
$this->load->helper('url');
$this->load->view('includes/header');
$this->load->view('my_records', $data);
$this->load->view('includes/footer');
}
// If everything is correct add's user.
else
{
$this->load->view('includes/header');
$this->load->view('my_records', $data);
$this->load->view('includes/footer');
}
}
} ?>
and view -
<div class="list-products">
<div>
<?php if(validation_errors()) { ?><div class="error-submit"><p><?php echo validation_errors(); ?></p></div><?php } ?>
<form class="fix-this form" method="post" action="/savieno/records/postRecord">
<div class="formfield">
<span class="profile">Your message:</span>
<textarea id="message" name="message" class="with-label" rows="10" cols="10" placeholder="your message."></textarea>
</div>
<div class="formfield">
<input type="submit" id="submit" name="postRecord" value="Pievienot" class="fix-this" />
</div>
</form>
</div>
At start, it gave me error, that can’t find functio set_message(), but I added form_validation before form helper, and now it works, but again there is problem, cause it doesn’t display form error codes. It just ain’t displaying it at all, I have set error message for it, and it should work, but it isn’t. What could be the problem?
In your view, you don't need:
<?php if(validation_errors()) { ?><div class="error-submit"><p><?php echo validation_errors(); ?></p></div><?php } ?>
You can use just:
<?php echo validation_errors('<div class="error-submit">', '</div>'); ?>
No need for the if statement there. From glancing over this, that might be able to solve your problem.
As a side note, in your Controller it's not necessary to have closing PHP tag, and it's actually considered best practice to leave it unclosed.
Oh, and you could if you wanted also make a construct for:
function __construct() {
parent::__construct();
$query = $this->db->get_where('users', array('uname' => $this->session->userdata('username')));
foreach($query->result_array() as $row) {
$data = array(
'username' => $row['uname'],
'email' => $row['email'],
'name' => $row['name'],
'description' => $row['description'],
'picture' => $row['picture'],
'gender' => $row['gender'],
);
}
}
Although, I think it would be better (MVC architecture) to just make that query available in a model (something) like this instead:
$this->load->model('dude');
$result = $this->dude->get_dude_where('$this->session->userdata('username'));
foreach($result as $row) {
$data = $result['result_array'];
}
But that would require you to setup a model function to pass/return $query->result_array().
Anyways, I hope this resolved your form_validation problem.