Undefined property: Mahasiswa::$form_validation
I already load the Form helper and Form Validation library.
I'm using CodeIgniter 3.1.10.
class Mahasiswa extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Mahasiswa_model');
$this->load->helper('form');
$this->load->library('form_validation');
}
public function index()
{
$data['judul'] = 'Data Mahasiswa';
$data['mahasiswa'] = $this->Mahasiswa_model->getAllMahasiswa();
$this->load->view('templates/header', $data);
$this->load->view('mahasiswa/index');
$this->load->view('templates/footer');
}
public function tambah()
{
$data['judul'] = "Form Tambah Data Mahasiswa";
$this->form_validation->set_rules('nama', 'Nama', 'required');
if ( $this->form_validation->run() == FALSE )
{
$this->load->view('templates/header', $data);
$this->load->view('mahasiswa/tambah');
$this->load->view('templates/footer');
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('mahasiswa');
$this->load->view('templates/footer');
}
}
}
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Mahasiswa::$form_validation
Filename: controllers/Mahasiswa.php
Line Number: 26
You should add the form_validation library in autoload.php in the config folder. because I also had the same issue and it worked for me
$autoload['libraries'] = array('form_validation');
First you should load libraries in autoload.php in the config folder. Like this
$autoload['libraries'] = array('form_validation);
$autoload['helper'] = array('url', 'file');
Related
I am a beginner in codeigniter and I am making a basic crud for learning. I am facing an error which is in index.php
<h2><?=$title?></h2>
<?php foreach($posts as $post)?>
<h3><?php echo $post['title'];?></h3>o
<small class="post-date">Posted on:<?php echo $post['created at'];?> </small><br>
<?php echo word_limiter($post['body'],60);?>
<p><a class="btn btn-default" href="<?php echo site_url('/posts/' .$post['slug']);?>">read more</a></p>
<?php endforeach;?>
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: posts
Filename: posts/index.php
Line Number: 2
Backtrace:
File: /opt/lampp/htdocs/codeigniter/application/views/posts/index.php
Line: 2
Function: _error_handler
File: /opt/lampp/htdocs/codeigniter/application/controllers/Posts.php
Line: 23
Function: view
File: /opt/lampp/htdocs/codeigniter/index.php
Line: 315
Function: require_once
my controller is
<?php
class Posts extends CI_Controller {
public function index(){
$data['title']='Latest Posts';
$data['posts']=$this->Post_model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function view($slug=NULL)
{
$data['post'] = $this->Post_model->get_posts($slug);
if(empty($data['post'])){
show_404();
}
$data['title']=$data['post']['title'];
$this->load->view('templates/header');
$this->load->view('posts/view', $data);
$this->load->view('templates/footer');
}
public function create(){
$data['title'] = 'create post';
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('body','body','required');
if($this->form_validation->run() === false) {
$this->load->view('templates/header');
$this->load->view('posts/create',$data);
$this->load->view('templates/footer');
} else {
$this->Post_model->create_post();
redirect('posts');
}
}
public function delete($id) {
$this->Post_model->delete_post($id);
redirect('posts');
}
public function edit($id) {
$data['post'] = $this->Post_model->get_posts($slug);
if(empty($data['post'])){
show_404();
}
$data['title'] = 'edit post';
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
public function update()
{
$this->Post_model->update_post();
redirect('posts');
}
}
my model file is
<?php
class Post_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_posts($slug = false){
if($slug === false) {
$this->db->order_by('id','DESC');
$query=$this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts',array('slug' => $slug));
return $query->row_array();
}
public function create_post(){
$slug=url_title($this->input->post('title'));
$data=array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
return $this->db->insert('posts',$data);
}
public function delete_post($id){
$this->db->where('id',$id);
$this->db->delete('posts');
return true;
}
public function update_post() {
$slug=url_title($this->input->post('title'));
$data=array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
$this->db->where('id',$this->input->post('id'));
return $this->db->update('posts',$data);
}
}
On your edit function on controller you have edit($id)
But where you load your model you have get_posts($slug) it should be get_posts($id)
And also change $data['post'] to $data['posts']
public function edit($id) {
$data['posts'] = array();
// rename slug to id
$data['posts'] = $this->post_model->get_posts($id);
if(empty($data['posts'])){
show_404();
}
$data['title'] = 'edit post';
$this->load->view('templates/header');
/*
Tip You don't have to use index.php on for view you could create a
another view instead $this->load->view('posts/edit_view',$data);
*/
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
On the when load model on controller
Change
$this->Post_model->get_posts($id);
To
$this->post_model->get_posts($id);
https://www.codeigniter.com/user_guide/general/models.html#loading-a-model
On Autoload.php
Change
$autoload['model'] = array('Post_model');
To
$autoload['model'] = array('post_model');
And load database in autoload.php make life easier
$autoload['libraries'] = array('database');
hey guys im going thru the news section tutorial on codeigniter and i get the following error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: News::$db
Filename: core/Model.php
Line Number: 77
Backtrace:
File: /Applications/MAMP/htdocs/CodeIgniter/application/models/News_model.php
Line: 13
Function: __get
File: /Applications/MAMP/htdocs/CodeIgniter/application/controllers/News.php
Line: 10
Function: get_news
File: /Applications/MAMP/htdocs/CodeIgniter/index.php
Line: 292
Function: require_once
All i did was simply copy what was provided in the tutorial, here are my files
news_model.php file
<?php
class News_model extends CI_Model {
public function __constrcut() {
$this->load->database();
}
public function get_news($slug = FALSE) {
if ($slug === FALSE ) {
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
news.php
<?php
class News extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
}
public function index() {
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header',$data);
$this->load->view('news/index',$data);
$this->load->view('templates/footer');
}
public function view($slug) {
$data['news'] = $this->news_model->get_news($slug);
if (empty($data['news_item'])) {
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header',$data);
$this->load->view('news/view',$data);
$this->load->view('templates/footer');
}
}
and here is a picture of my file structure
im trying my best to learn and debug but i dont know what it could be.
Maybe, database library is not loaded. you have to load database library.
application / config / autoload.php
$autoload['libraries'] = array('database');
I am working on the news section example as demonstrated in the link below:
http://www.codeigniter.com/userguide2/tutorial/news_section.html
It contains two functions in the controller index and view.
When I remove the view function from my controller even then I get the same output. Can any one of you help me understanding the need of function view in the controller?
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
How CodeIgniter URLS work is the following:
example.com/controller/method/param1[/param2...]
From: http://www.codeigniter.com/userguide2/general/urls.html
When you go to yoursite.com/news, it automatically runs the index() function. But, what if you went to yoursite.com/news/view/1234?
Then it would run your view() function and pass '1234' as a parameter ($slug).
im currently trying to change my application from MySQL database to MongoDB database. My project requires to compare two of these, and my MySQL code was working and now it gives me this error.
Stack Trace:
A PHP Error was encountered Severity: Notice Message: Undefined property: Users::$form_validation Filename: controllers/users.php Line Number: 26
Error line:
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');
Code:
<?php
class Users extends CI_Controller{
public function register(){
$this->form_validation->set_rules('first_name','First Name','trim|required|max_length[50]|min_length[2]|xss_clean');
$this->form_validation->set_rules('last_name','Last Name','trim|required|max_length[50]|min_length[2]|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|max_length[100]|min_length[5]|xss_clean|valid_email');
$this->form_validation->set_rules('username','Username','trim|required|max_length20]|min_length[6]|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|max_length[20]|min_length[6]|xss_clean');
$this->form_validation->set_rules('password2','Confirm Password','trim|required|max_length[50]|min_length[2]|xss_clean|matches[password]');
if($this->form_validation->run() == FALSE){
$data['main_content']='users/register';
$this->load->view('layouts/main',$data);
}else{
if($this->User_model->create_member()){
$this->session->set_flashdata('registered','Registered successfully');
redirect('home/index');
}
}
}
public function login(){
//$this->load->model('user_model');
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[50]|xss_clean');
$collection = $this->database->users;
if($this->form_validation->run() == FALSE){
$this->session->set_flashdata('login_failed', 'Sorry, this username doesn't exist.');
redirect('home/index');
} else {
$username=$_POST['login'];
$password=$_POST['password'];
$user_id = $collection->findOne(array('username' => $username, 'password' => md5($password)));
// $username = $this->input->post('username');
// $password = $this->input->post('password');
$user_id = $this->User_model->login_user($username,$password);
if($user_id){
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata($user_data);
redirect('home/index');
} else {
$this->session->set_flashdata('login_failed', 'Sorry, this username doesn't exist.');
redirect('home/index');
}
}
}
public function logout(){
$this->session->unset_userdata('logged_in');
$this->session->unset_userdata('user_id');
$this->session->unset_userdata('username');
$this->session->sess_destroy();
redirect('home/index');
}
}
Changing config/autoload.php worked for me, just add form_validation in autoload :
$autoload['libraries'] = array('form_validation');
This because of failed to load relevant library
Method 01
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
Method 02
in config/autoload.php
$autoload['libraries'] = array('form_validation');
$autoload['helper'] = array('url', 'form');
Form_Validation Codeigniter
Follow this method-
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
Hello I am pretty new to codeIgniter and I've created a contact page with controller and a view.. the only problem is that I don't get anything when I go to the link http://www.mydomain.nl/index.php/contact
My controller is as follows
<?php
class Contact extends CI_Controller {
public function index()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Contact';
$this->form_validation->set_rules('name', 'Naam', 'trim|required');
$this->form_validation->set_rules('beschrijving', 'Beschrijving', 'trim');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email')
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('contact/index');
$this->load->view('templates/footer');
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('contact/formsucces');
$this->load->view('templates/footer');
}
}
}
with this controller i've also created a folder in my view called contact
in this folder i've got 2 files the index.php and a formsucces.php for when the form is beeing submitted.
My question is what am i doing wrong? i've looked all the examples I could find but nothing helps..
edit: added more code
This is a controller for the news section. the controller and the view is the same as the contact controller and view.
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
and to be really really sure that my directory, paths etc are correct here is the print screen of my directory