I am checking a country name already exist in my database i used callback but it doest not shoot any error
$this->form_validation->set_rules('country_name', 'Country Name',
'trim|required|xss_clean|callback_check_exist');
This is my controller function
function check_exist($country_name)
{
$this->countrymodel->country_exists($country_name);
}
and this is my model
function country_exists($key)
{
$this->db->where('country_name',$key);
$this->db->from($this->dbname);
$query = $this->db->get();
if ($query->num_rows()> 0){
return true;
}
else{
return false;
}
}
The callback function should return a value and set a message:
function check_exist($country_name)
{
if (!$this->countrymodel->country_exists($country_name)) {
$this->form_validation->set_message('check_exist', 'an error message');
return FALSE;
}
else {
return TRUE;
}
}
Related
I want to validate login form with ajax. I am using form valiation with a callback function that check username in database. When I am using callback with set_message, form validation errors are not working, only that callback error message is shown. If username is empty then form validation error like "Username is required" should be shown first and then if user enter wrong username then callback function error like "Username is not correct" should be shown
Following are the functions in my controller
public function validate_form()
{
$data = array('success' => false, 'messages' => array());
$this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean|callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required|trim|xss_clean|callback_password_check');
$this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
if ($this->form_validation->run()) {
$data['success'] = true;
$this->session->set_userdata('admin_username', $this->input->post('username'));
} else {
foreach ($_POST as $key => $value) {
$data['messages'][$key] = form_error($key);
}
}
echo json_encode($data);
}
public function username_check()
{
$username = $this->input->post("username");
if ($this->admin_model->usernameDB()) {
return true;
} else {
$this->form_validation->set_message('username_check', 'The {field} is not correct');
return false;
}
}
public function password_check()
{
$password = $this->input->post("password");
if ($this->admin_model->passwordDB($password)) {
return true;
} else {
$this->form_validation->set_message('password_check', 'The {field} is not correct');
return false;
}
}
Following are the function in my model
public function usernameDB() {
$this->db->where('username', $this->input->post('username'));
$query = $this->db->get('adminuser');
if ($query->num_rows() == 1) {
return true;
} else {
return false;
}
}
public function passwordDB() {
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('adminuser');
if ($query->num_rows() == 1) {
return true;
} else {
return false;
}
}
Following is the ajax that i am using
$("#admin_login_form").submit(function(e) {
e.preventDefault();
var me = $(this);
// perform ajax
$.ajax ({
url: "validate_form",
type: "post",
data: me.serialize(),
dataType: "json",
success: function(response) {
if (response.success == true) {
$('.form-group').removeClass('has-error')
.removeClass('has-success');
$('.text-danger').remove();
window.location = "member";
} else {
$.each(response.messages, function(key, value) {
var element = $('#' + key);
element.closest('div.form-group')
.removeClass('has-error')
.addClass(value.length > 0 ? 'has-error' : 'has-success')
.find('.text-danger')
.remove();
element.after(value);
});
}
}
});
});
I want proper validation errors order like if username or password is empty then first their relevant errors should be shown like username or password is required and then wrong or correct username or password errors should be shown.
You can try below. You can use single callback function to check whether user is exists or not.
First, remove the callback functions public function username_check() and public function password_check() from your controller and replace with the updated functions below.
In addition to this created new model function login inside admin_model which will check whether user is exists or not. Also you can delete both usernameDB and passwordDB from your admin_model.
Controller function:
public function validate_form()
{
$data = array('success' => false, 'messages' => array());
$this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|trim|xss_clean|is_user_exists');
$this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
if ($this->form_validation->run()) {
$data['success'] = true;
$this->session->set_userdata('admin_username', $this->input->post('username'));
} else {
foreach ($_POST as $key => $value) {
$data['messages'][$key] = form_error($key);
}
}
echo json_encode($data);
}
public function is_user_exists()
{
if ($this->admin_model->login($this->input->post('username', TRUE), $this->input->post('password', TRUE))) {
return true;
} else {
$this->form_validation->set_message('is_user_exists', 'Login failed. Username or password is incorrect');
return false;
}
}
Model function:
public function login($username, $password)
{
$this->db->where('username', $username);
$this->db->where('password', md5($password));
$query = $this->db->get('adminuser');
if ($query->num_rows() > 0) {
return true;
} else {
return false;
}
}
Note: Using md5 for encrypt password is not good. See Alex's comment for more details.
Fatal error: Uncaught Error: Call to a member function check_main_module() on null on line 390
I have added a constructor and in the config, I have added modules as the library also.
function is_main_module_enabled($module) {
$result = $this->Modules_model->check_main_module($module);
return $result;
}
**`In Modules_model.php`**
function check_main_module($module) {
$this->load->library('ptmodules');
return $this->ptmodules->is_main_module_enabled($module);
}
**`In ptmodules file`**
function is_main_module_enabled($module) {
$this->_ci->db->select('page_id');
$this->_ci->db->where('page_status', 'Yes');
$this->_ci->db->where('page_slug', $module);
$rows = $this->_ci->db->get('pt_cms')->num_rows();
if ($rows > 0) {
return true;
}
else {
return false;
}
}
Try This,
Controller:
function is_main_module_enabled($module) {
$this->load->model('Modules_model');
$result = $this->Modules_model->check_main_module($module);
return $result;
}
Library:
function is_main_module_enabled($module) {
$CI = & get_instance();
$CI->db->select('page_id');
$CI->db->from('pt_cms');
$CI->db->where('page_status', 'Yes');
$CI->db->where('page_slug', $module);
$rows = $CI->db->get();
if ($rows->num_rows() > 0) {
return true;
}
else {
return false;
}
}
I currently have a relationship between checklistItem and actions as followed:
public function action()
{
return $this->belongsTo(Action::class, 'action_id', 'id');
}
public function checklistItem()
{
return $this->hasOne(ChecklistItem::class, 'action_id', 'id');
}
I currently made a method, when saving an action, the checklistItem status should also change depending on what was chosen:
public static function saveFromRequest(Request $request)
{
if (($action = parent::saveFromRequest($request)) !== null){
if (!is_null($action->checklistItem)) {
$action->checklistItem->value->status = $action->switchStatusChecklist($action);
//Need to update or save this specific checklistItem
$action->checklistItem->save();
}
}
return $action;
}
function switchStatusChecklist($action)
{
switch($action->status) {
case 'closed':
$status = 'checked';
break;
case 'canceled':
$status = 'notapplicable';
break;
default:
$status = 'open';
break;
}
return $status;
}
Problem:
My checklistitem does not get updated.
In the view it is getting the error like undefined variable categorylist,can u
pls help me !!!!!!!
Controller
public function categorylist()
{
$data['categorylist']=$this->userprocessmodel->categorylist();
$this->load->view('user/home',$data);
}
Model
public function categorylist()
{
$query=$this->db->get('category');
echo $query->num_rows();
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return null;
}
}
view
<?php
if($categorylist)
{
foreach($categorylist as $categorylist)
{
$categorylist->categoryname;
}
}
?>
Just change these code in model
public function categorylist()
{
$query=$this->db->get('category');
echo $query->num_rows();
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return 0;
}
}
Now in controller if 0 get then
public function categorylist()
{
$data['categorylist']=$this->userprocessmodel->categorylist();
if($data['categorylist']=='0')
{
$datanone['categorylist']='None';
$this->load->view('user/home',$datanone);
}
else{
$this->load->view('user/home',$data);
}
}
Your view should be like this
<?php
if(count($categorylist)>0)
{
foreach($categorylist as $categorylist)
{
$categorylist->categoryname;
}
}else{
echo 'No category found';
}
?>
Also you dont need to echo $query->num_rows()
In your model, you must change:
return null;
With:
return false;
If you assign null to variable $categorylist, the variable is not initialized, and you get that error in your view, if you assing false instead, the variable is initialized with the boolean value false.
My custom function is not working. I am checking if passed value does not exist in the database it return error message. What I am doing wrong?
Controller function
function sp_exists($str)
{
$this->user_model->sp_exists($str);
$this->form_validation->set_message('sp_exists', 'The %s field does not exists');
}
Model function
function sp_exists($str)
{
$this->db->where('member_id',$str);
$query = $this->db->get('user');
if ($query->num_rows() > 0)
{
return false;
}
else
{
return true;
}
}
callback function
$this->form_validation->set_rules('sponsor_id', 'Sponsor ID', 'trim|required|xss_clean|callback_sp_exists');
Did you look at the user guide? It explains clearly how to do this.
First of all I think you should change the model function to this:
function sp_exists($str)
{
$this->db->where('member_id',$str);
$query = $this->db->get('user');
return $query->num_rows() > 0;
}
And the controller should look like this:
function sp_exists($str)
{
if(!$this->user_model->sp_exists($str))
{
$this->form_validation->set_message('sp_exists', 'The %s field does not exists');
return FALSE;
}
else
{
return TRUE;
}
}
change
function sp_exists($str)
{
$this->user_model->sp_exists($str);
$this->form_validation->set_message('sp_exists', 'The %s field does not exists');
}
to
function sp_exists($str)
{
$this->form_validation->set_message('sp_exists', 'The %s field does not exists');
return $this->user_model->sp_exists($str);
}