Validation flashdata error is not displaying in CodeIgniter - php

Here is my login method inside the controller, Here I am setting a flash message for the validation errors -
public function login(){
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[3]');
if($this->form_validation->run() == FALSE){
$data = array(
'errors' => validation_errors()
);
$this->session->set_flashdata($data);
redirect('home');
}
}
Here id the code to display those errors -
<?php if($this->session->flashdata('errors')): ?>
<?php echo $this->session->flashdata('errors');?>
<?php endif; ?>
I don't know what is wrong, Error message is not displaying.

I hope work this process
if($this->form_validation->run() == FALSE){
$this->session->set_flashdata('name','Your message');
redirect('home');
}
<?php if($this->session->flashdata('name')): ?>
<?php echo $this->session->flashdata('name');?>
<?php endif; ?>

Its placed in controller
if ($this->form_validation->run() == FALSE) {
$errors = validation_errors();
$this->session->set_flashdata('form_error', $errors);
$this->load->view('Your View page');
}
Its placed in your view page
<?php if($this->session->flashdata('form_error')): ?>
<?php echo $this->session->flashdata('form_error');?>
<?php endif; ?>
I hope this it will help to you..!

Related

How to compare form input data to database data in a custom codeigniter form validator?

Edit: Some naming had been mixed up in my attempts to solve it myself. I've fixed the callback etc naming and the same error persists.
I am attempting to create a login page for my codeigniter website. I already have a registration page that correctly inputs usernames and passwords in to a "users" table. I am having issues understanding the syntax of creating the functions needed for custom form validators.
My error is "Unable to access an error message corresponding to your field name" for the password and username custom validators.
Here is the relevant part of the controller "login_ctrl"
class login_ctrl extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('login_mdl');
}
function index() {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
//Validating Name Field
$this->form_validation->set_rules('username', 'Username', 'trim|required|callback_userCorrect');
//Validating Password Field
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_passwordCorrect');
//variables to pass form input
$username = $this->input->post('username');
$password = $this->input->post('password');
//reload login page if validation fails
if ($this->form_validation->run() == FALSE) {
$this->load->view('login');
} else {
//functions for custom validation
function userCorrect($username) {
$this->load->library('form_validation');
//the loads the model that contains the function to compare input to database data
$userExists = $this->login_mdl->userExists($username);
if ($userExists) {
$this->form_validation->set_message(
'userCorrect', 'correct user.'
);
return true;
} else {
$this->form_validation->set_message(
'userCorrect', 'not a valid user name.'
);
return false;
}
}
function passwordCorrect($password) {
$this->load->library('form_validation');
$passwordExists = $this->login_mdl->passwordCorrect($password);
if ($passwordExists) {
$this->form_validation->set_message('passwordCorrect', 'correct password.');
return true;
} else {
$this->form_validation->set_message('passwordCorrect', 'invalid password.');
return false;
}
}
This is the corresponding view "login"
<?php echo form_open('login_ctrl'); ?>
<h1>Login</h1><hr/>
<?php if (isset($message)) { ?>
<?php } ?>
<?php echo form_label('User Name :'); ?> <?php echo form_error('username'); ?><br />
<?php echo form_input(array('id' => 'username', 'name' => 'username')); ?><br />
<?php echo form_label('Password :'); ?> <?php echo form_error('password'); ?><br />
<?php echo form_input(array('id' => 'password', 'name' => 'password')); ?><br />
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?><br/>
Finally, this is the corresponding model "login_mdl" (I think the issue might be in this guy).
<?php
class login_mdl extends CI_Model{
function __construct() {
parent::__construct();
}
function userExists($username) {
$this->db->select('id');
$this->db->where('username', $username);
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return true;
} else {
return false;
}
}
function passwordCorrect($password) {
$this->db->select('password');
$this->db->where('username', $username);
$query = $this->db->get('users');
$result = $query->row();
return $result->password;
if ('password' == $password) {
return true;
} else {
return false;
}
}
}
?>
I think my issue is related to the db calls and if statements but I've been reading documentation and failing at fixing this for hours so a new pair of eyes would be greatly appreciated.
You need to make your fieldname on your costume rules function is same as your function callback. So, it’s should be like this :
$this->form_validation->set_message(
'userCorrect', 'correct user.'
Do same thing on your other function.

Can't display Codeigniter validation error

Don't get this.
I have to validate basic form. Im using CI form_validation library, form validation rules are set just to "required"...if form validation fails it just need to echo validation error in login form. Im using by default <?php echo validation_errors(); ?> Helper is autoloaded. Form functionality is OK it works how it should, only problem is that framework dont display error messagges if dont fill required fields?
login_form.php - VIEW
<?php echo validation_errors(); ?>
<?php echo form_input('email' , 'email'); ?> <br>
<?php echo form_input('password' , 'password'); ?> <br>
<?php echo form_submit('submit' , 'submit'); ?>
login.php - CONTROLLER
<?php
Class Login extends CI_Controller {
function index () {
$data ['content'] = "login_form";
$this->load->view("template", $data);
}
function validate() {
$this->load->library("form_validation");
$this->form_validation->set_rules('email' , 'email' , 'trim|xss_clean|required');
$this->form_validation->set_rules('password' , 'password' , 'md5|trim|required');
if( $this->form_validation->run() == FALSE) {
redirect('login/index');
}
else {
redirect("user/profile");
}
}
}
?>
Why don't you use code in controller like this hope will help you just set form action to login.php
login.php - CONTROLLER
function index () {
$data ['content'] = "login_form";
$this->load->library("form_validation");
$this->form_validation->set_rules('email' , 'email' , 'trim|xss_clean|required');
$this->form_validation->set_rules('password' , 'password' , 'md5|trim|required');
if( $this->form_validation->run() == FALSE) {
$this->load->view("template", $data);
}
else {
redirect("user/profile");
}
}
login_form.php - VIEW
<?php echo validation_errors(); ?>
<?php echo form_input('email' , 'email'); ?> <br>
<?php echo form_input('password' , 'password'); ?> <br>
<?php echo form_submit('submit' , 'submit'); ?>
if( $this->form_validation->run() == FALSE) {
redirect('login/index');
}
Don't redirect, else error will not be available. Instead of that keep
if( $this->form_validation->run() == FALSE) {
$data ['content'] = "login_form";
$this->load->view("template", $data);
}
Check the following link for more details:
Form Validation : CodeIgniter User Guide

Form validation is not working in codeigniter

I am working with codeigniter. I have created one function to add university form.
function add_uni_admin()
{
$user_id = $this->session->userdata("user_id");
if (!empty($user_id)) {
$uni_name = $this->input->post("uni_name");
$uni_image = $this->input->post("uni_image");
$uni_email = $this->input->post("uni_email");
$phn_no = $this->input->post("phn_no");
$m_no = $this->input->post("m_no");
$address = $this->input->post("address");
$password = $this->input->post("pass");
$this->form_validation->set_rules('uni_name', 'University Name', 'required');
$this->form_validation->set_rules('uni_email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('phn_no', 'Phone Number', 'required');
$this->form_validation->set_rules('m_no', 'Mobile Number', 'required');
$this->form_validation->set_rules('address', 'Address', 'required');
$this->form_validation->set_rules('pass', 'Password', 'required|matches[conf_pass]');
$this->form_validation->set_rules('conf_pass', 'Confirm Password', 'required');
if ($this->form_validation->run() == false) {
$this->load->view("admin/add_uni_admin");
} else {
echo "successfull";exit;
}
} else {
redirect(base_url());
}
}
When I submit the form without filling any field then it does not give any error.Here I have added form_validation library into autoload.php file and added form helper in it.
So what code should I have to write to display errors?
use echo validation_errors() in your view to display errors.
<html>
<head>
<title>My Form</title>
</head>
<body>
<?php echo validation_errors(); ?>
**your form here.**
</body>
</html>
Add $this->load->library('form_validation');
in your controller function..
And <?php echo validation_errors(); ?>
in your view file to display errors.

Codeigniter forms won't post

For some reason I can't get forms to post in Codeigniter 2.1.4, I honestly have no clue where I am going wrong. Can anyone see any obvious problems? No post data is returned just an empty form the vardump returns (boolean false)
public function testpost()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
var_dump($this->input->post());
$this->form_validation->set_rules('testt', 'Test', 'required');
if ($this->form_validation->run() == FALSE)
{
echo validation_errors();
echo form_open('test/testpost');
echo form_input(array('type'=>'text', 'id'=>'testt'));
echo form_submit(array('value' => 'submit'));
echo form_close();
}
else
{
echo "true";
}
}
You did not create name of text field.
echo form_input(array('type'=>'text', 'id'=>'testt', 'name'=>'testt'));

Codeigniter 2.1 send data from one controller to another

I have function like this:
function gi_insert()
{
if(!IS_AJAX){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags');
$this->form_validation->set_rules('body', 'Tekst', 'trim|required|strip_tags');
$this->form_validation->set_rules('email', 'E-mail', 'trim|required|strip_tags|valid_email');
$this->form_validation->set_rules('tag', 'Tagovi', 'trim|required|strip_tags');
if(isset($_POST['category_new'])){
$this->form_validation->set_rules('category_new', 'Kategorija', 'trim|strip_tags');
}
$p = $this->uri->segment(3);
if ($this->form_validation->run() == FALSE)
{
$errors = validation_errors();
redirect("admin/create/$p", 'location');
}
else
{
$this->gi->gi_insert();
redirect('admin/pregled/' . $this->uri->segment(3));
}
} else
{
$this->gi->gi_insert();
}
}
How can I send validation errors to admin/create controller? At the moment, this is working, but I don't get error report (page where errors should appear is containig
<?php echo validation_errors(); ?>
)
validation_errors() only works if the $_POST data is still set, since you redirect to admin/create, $_POST is empty and the function no longer works, returning nothing.
You could send the errors with a piece of flashdata to the page.

Categories