Codeigniter validation is not working - php

I am new in codeigniter .I have done validation for my project .But it is not working fine .I have written my all code here .First is view page and second is my controller page.Please help anyone
<?php $this->load->helper('form');
echo validation_errors();
echo form_open('SM_in_controller/sm_login_action');
?>
<input class="login_input" type="text" placeholder="Username" name="username" id="username"/>
<input type="submit" value="Login" class="login_button" id="login_button"/>
</form>
and my controller
public function sm_login_action() {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
}

In your controller, when you call the "run" method is when the proccess is done:
public function sm_login_action() {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
if( $this->form_validation->run() ) { // Return TRUE on success
// Success
} else {
// Failure
}
}

<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
try this for more details.
https://www.codeigniter.com/user_guide/libraries/form_validation.html

Try this
<input class="login_input" type="text" placeholder="Username" name="username" id="username" required/>
Hope this helps

Related

How to display the error on each fields in codeigniter

$this->form_validation->set_rules('username', 'User Name', 'required|min_length[4]|is_unique[users.username]');
$this->form_validation->set_rules('fname', 'Full Name', 'required|min_length[4]');
$this->form_validation->set_rules('user_password', 'Password', 'required|matches[confirm_password]');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required');
$this->form_validation->set_rules('cname', 'College Name', 'required');
$this->form_validation->set_rules('dob', 'Date of Birth', 'required|callback_valid_date');
$this->form_validation->set_rules('addr', 'Address', 'required');
$this->form_validation->set_rules('contact', 'Contact', 'required|min_length[10]|max_length[10]');
$this->form_validation->set_rules('education', 'Education', 'required');
$this->form_validation->set_rules('user_email', 'Email', 'required|valid_email|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('result', validation_errors());
redirect('login/register');
}
else
{
$rr = $this->user->register_user();
$this->session->set_flashdata('result', $rr);
redirect('login/register');
}
}
basically using $this->session->flashdata("result"); to show the result but i want each filed seprately validate i have tried form_error("field name "); but it's not working displaying empty result
Instead of redirecting, try to load the same form if there are any validation errors.
In your controller page.
function your_function(){
// load form helper
$this->load->helper(array('form', 'url'));
// load form validation library
$this->load->library('form_validation');
// validation rules
$this->form_validation->set_rules('username', 'User Name', 'required|min_length[4]|is_unique[users.username]');
$this->form_validation->set_rules('fname', 'Full Name', 'required|min_length[4]');
// check form for validations
if ($this->form_validation->run() == FALSE){
// load the same form/view
$this->load->view('myform');
} else{
// redirect to success page
}
}
In view page: can use the form_error() function to show an error message next to each form field
<?php echo form_error('username'); ?>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
<?php echo form_error('fname'); ?>
<input type="text" name="fname" value="<?php echo set_value('fname'); ?>" size="50" />

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.

Cant return error message in CodeIgniter 3 callback

I have a problem in my code. I am creating a simple login using CI3 for my small project. My problem is I have an error message in callback validation.
Here's the error I received whenever I try to validate my form.
Unable to access an error message corresponding to your field name Password.(check_database)
Here's mo code in controller:
public function index() {
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
$this->form_validation->set_error_delimiters('<div class="error text-red">', '</div>');
if($this->form_validation->run() == FALSE) {
$data = array();
$data['modules'] = $this->flx_lib->moduler($data);
$this->load->view('login', $data);
} else {
//ok
}
}
public function check_database($password) {
$username = $this->input->post($username);
$result = $this->flx_users->validate_user($username, $password);
if($result) {
//ok
return TRUE;
} else {
$this->form_validation->set_message('check_database', 'Invalid username or password');
return FALSE;
}
}
Here's my view:
<div class="form-group has-feedback <?php error_exists('username'); ?>">
<input type="text" class="form-control" name="username" placeholder="Username" />
<span class="fa fa-user form-control-feedback "></span>
<?php echo form_error('username'); ?>
</div>
<div class="form-group has-feedback <?php error_exists('password'); ?>">
<input type="password" class="form-control" name="password" placeholder="Password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
<?php echo form_error('password'); ?>
</div>
Can you help me with this? I am using CI3 with HMVC
Ok I found the error in my code. Actually its an issue with HMVC form validation.
Here's the link for the correct answer:
CodeIgniter - Unable to access an error message corresponding to your field name Password.(pword_check)
Solution:
1. Create MY_Form_validation.php file in libraries folder and paste following code in it.
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
function run($module = '', $group = '')
{
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
And change if ($this->form_validation->run() == FALSE) to if ($this->form_validation->run($this) == FALSE) thats all folks..
public function index() {
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database',
array('check_database' => 'Invalid username or password.')
);
$this->form_validation->set_error_delimiters('<div class="error text-red">', '</div>');
if($this->form_validation->run() == FALSE) {
$data = array();
$data['modules'] = $this->flx_lib->moduler($data);
$this->load->view('login', $data);
} else {
//ok
}
}
public function check_database($password) {
$username = $this->input->post($username);
$result = $this->flx_users->validate_user($username, $password);
if($result) {
//ok
return TRUE;
} else {
return FALSE;
}
}
I recoded this one. Something was mixed up.
This is from docs:
$this->form_validation->set_rules('field_name', 'Field Label', 'rule1|rule2|rule3',
array('rule2' => 'Error Message on rule2 for this field_name')
);

Codelgniter Form Validation Showed double repeated form after validate

Tried to read though all the webpages and docs in google and stack flow but still could not solve the problem.
I tried to do a simple data validation for registration form and it turns out showing another form below the original one after I press submit to show the error messages with a new form.
I am a newbie in this language so please let me know if I attache not enough codes or information.
Controller account:
<?php
class Account extends MY_Controller {
public function __construct() {
parent::__construct();
session_start();
$this->load->model('user');
$this->load->helper(array('form','url','html'));
$this->load->library('session');
$this->load->library('form_validation');
}
public function registration() {
$data = $this->user->users_info();
$this->load->view('account/registration',$data);
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[20]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password ', 'required|matches[passconf]|min_length[5]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
if($this->input->post('submit')) {
$username= $this->input->post('username');
$email= $this->input->post('email');
$query_u= $this->user->retrieve_by_username($username);
$query_e= $this->user->retrieve_by_email($email);
if ($this->form_validation->run() == FALSE){
$this->load->view('account/registration',$data); ←---------------- (I think this is wrong, it makes load the second form out.)
}
else{
if(!empty($query_u) or !empty($query_e)) {
redirect('account/registrat');
}
else {
$data = array(
'username'=>$this->input->post('username'),
'email'=>$this->input->post('email'),
'password'=>$this->input->post('password'),
'is_admin'=>0,
);
$this->user->create_user($data);
redirect('/account/login');
}
}
}
}
View Registration.php
<center>
<?php echo form_open_multipart('account/registration'); ?>
<h5><?php echo $b_username;?> (Minimum 5 characters)</h5>
<input type="text" name="username" id="username" value="<?php echo set_value('username'); ?>" size="50" /><?php echo form_error('username'); ?>
<h5><?php echo $b_email;?></h5>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<?php echo form_error('email'); ?>
<h5><?php echo $b_password;?> (Minimum 5 characters)</h5>
<input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />
<?php echo form_error('password'); ?>
<h5><?php echo $b_passconf;?></h5>
<input type="text" name="passconf" value="" size="50" />
<?php echo form_error('passconf'); ?>
<h5></h5>
<div><?php echo form_submit('submit', 'Submit') ?></div>
</center>
Model user.php
<?php
class User extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function users_info() {
$data['b_id'] = 'id';
$data['b_username'] = 'Username';
$data['b_email'] = 'Email';
$data['b_password'] = 'Password';
$data['b_passconf'] = 'Enter Password Again';
$data['b_is_admin'] = 'Is_admin';
$data['b_default_privacy'] = 'Default_privacy';
$data['b_first_name'] = 'First_Name';
$data['b_last_name'] = 'Last_Name';
$data['b_gender'] = 'Gender';
$data['b_birthday'] = 'Birthday';
$data['b_friend_id'] = 'Friend_id';
$data['b_weight'] = 'Weight';
$data['b_height'] = 'Height';
$data['b_daily_cal_intake'] = 'Daily_calorie_intake';
$data['b_target_weight'] = 'Target_weight';
$data['b_regional_id'] = 'Region';
$data['b_profile_pic'] = 'Profile Picture';
return $data;
}
function retrieve_by_username($username) {
$query = $this->db->get_where('001_users',array('username'=>$username));
return $query->row_array();
}
function retrieve_by_email($email) {
$query = $this->db->get_where('001_users', array('email'=>$email));
return $query->row_array();
}
Change your function to this and try..
public function registration()
{
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[20]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password ', 'required|matches[passconf]|min_length[5]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
if ($this->form_validation->run() == false) // if validation fails for first time and every time when ever condtion is not satisified
{
$data = $this->user->users_info();
$this->load->view('account/registration',$data);
}
else
{
$username= $this->input->post('username');
$email= $this->input->post('email');
$query_u= $this->user->retrieve_by_username($username);
$query_e= $this->user->retrieve_by_email($email);
if(!empty($query_u) or !empty($query_e)) {
redirect('account/registrat');
}
else {
$data = array(
'username'=>$this->input->post('username'),
'email'=>$this->input->post('email'),
'password'=>$this->input->post('password'),
'is_admin'=>0
);
$this->user->create_user($data);
// send sucess msg here
redirect('/account/login');
}
}
}

Codeigniter Validation form not working

I know that this problem is in some other question, but I can't figure it out how to handle it properly in my case. I've been searching for a while, and I can't find the solution:
I want to do one form and connect with my database, but something goes wrong and it's impossible to receive the values on the database.
Here it's the view (location: /application/view/pages/home.php):
<?php echo validation_errors();?>
<div id="signup" style='position: absolute; left:900px; top: 380px;'>
<?php echo form_open('user/signup');?>
<input type='text' required="required" placeholder="Full Name" name="fullname"/><br/>
<input type='text' required="required" placeholder="Email" name="email"/><br/>
<input type='text' required="required" placeholder="Password" name="password"/><br/>
<input type='text' required="required" placeholder="Confirm password" name="confirmpassword"/><br/>
<button type="submit" name="submit_signup">Sign up</button>
<?php echo form_close(); ?>
Here the controller (location: /application/controllers/user.php):
class User extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('User_model');
}
public index(){
$this->load->view('view');
}
public function signup(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('User_model');
$user_id = DEFAULT;
$data = array(){
'user_id' => $user_id,
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'full_name' => $this->input->post('fullname')
}
$this->User_model->add_user($data);
$this->index();
}
}
And the model (location: /application/models/user_model.php):
<?php
class User_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function add_user($data){
$this->load->database();
$this->db->insert('user',$data);
}
}
?>
You have syntax error in your array:
It should be:
$data = array(
'user_id' => $user_id,
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'full_name' => $this->input->post('fullname')
)
If still didn't work, your $user_id is probably an INT type, so just give it some INT.
And you are not doing any validation at all!?
Your Solution is here:
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}?>

Categories