I have a controller with:
if($_POST) {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$val = $this->form_validation;
$val->set_rules('content[title]', 'Title', 'trim|required');
$val->set_rules('content[subtitle]', 'Subtitle', 'trim');
$val->set_rules('content[description]', 'description', 'trim');
if ($val->run() AND $this->db->insert('content', $content)) {
// query done
}
}
When I post form I am getting this error:
Fatal error: Call to undefined method stdClass::load() in ...\libraries\Form_validation.php on line 450
Line of 450 of Form_validation.php
// Load the language file containing error messages
$this->CI->lang->load('form_validation');
Please help me fix this....
replace your input names with title, subtitle, description and try this code
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
if ($this->input->post()) {
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('subtitle', 'Subtitle', 'trim');
$this->form_validation->set_rules('title', 'Description', 'required');
$data['content'] = array(
'db_field_name' => $this->input->post('title'),
'db_field_name' => $this->input->post('subtitle'),
'db_field_name' => $this->input->post('description'),
);
if ($this->form_validation->run()){
$this->db->insert('content', $data['content']);
}
}
If you are using form validation and all for multiple instance you can define the helper and library in the config/autoload.php. Also refer the input_field name for validation rule.
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
if ($this->input->post()) {
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('subtitle', 'Subtitle', 'trim');
$this->form_validation->set_rules('title', 'Description', 'required');
if ($this->form_validation->run() == TRUE) {
//Your DB operation
$this->data['formdata'] = array(
'db_field_title' => $this->input->post('title'),
'db_field_subtitle' => $this->input->post('subtitle'),
'db_field_description' => $this->input->post('description')
);
$this->db->insert('content', $this->data['content']);
} else {
//Show error message
echo validation_errors();
}
}
In your case, i think there is missing of language file
You have to check that in system/language/english folder there is form_validation_lang.php is available or not. there is missing of form_Validation_lang.php you have to put that file
Related
Hi i am new here and i dont know how to use codeigniter and now im confused. So i am currently trying to add user data to the database using codeigniter 3.1.10 . When i click the " save " button there's nothing to display. The page was refresh
Can you help me please?
Models:
function add_user($data) {
$this->db->set("username",$data["username"]);
$this->db->set("password",$data["password"]);
$this->db->set("indirizzo",$data["indirizzo"]);
$this->db->set("citta",$data["citta"]);
$this->db->set("cap",$data["cap"]);
$this->db->insert("user");
$ins_id =$this->db->insert_id();
return $ins_id;
}
Controllers:
function add() {
$this->load->library('form_validation');
$this->form_validation->set_rules('save', '', 'trim|required|number');
if ($this->form_validation->run()) :
$data = array(
"username"=>$this->input->post("username"),
"password"=>$this->input->post("password"),
"indirizzo"=>$this->input->post("indirizzo"),
"citta"=>$this->input->post("citta"),
"cap"=>$this->input->post("cap"),
);
$user_id= $this->user_model->add_user($data);
$this->log_model->scrivi_log($user_id,"user","add");
$this->session->set_flashdata('feedback', 'User added.');
redirect("user/pageuser/".$user_id);
else :
$content = $this->view->load("content");
$content->load("clienti_form","user/add");
$this->view->render();
endif;
}
Your doing a lot wrong, starting from the fact that your doing stuff from the model in your controller, and you should divide it, otherwise your not using the concept of MVC.
Try something like this, being hard to help you, without seeing the whole code:
Model
function add_user()
{
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'indirizzo' => $this->input->post('indirizzo'),
'citta' => $this->input->post('citta'),
'cap' => $this->input->post('cap')
);
return $this->db->insert('user', $data);
}
Controller
function add() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('indirizzo', 'Indirizzo', 'required');
$this->form_validation->set_rules('citta', 'Citta', 'required');
$this->form_validation->set_rules('cap', 'Cap', 'required');
$errore = true;
if ($this->form_validation->run() === FALSE){ // if doesnt work load your view
$this->load->view('your view');
}
else {
$this->user_model->add_user();
$this->log_model->scrivi_log($user_id,"user","add");
$this->session->set_flashdata('feedback', 'User added.');
redirect("user/pageuser/".$user_id);
$content = $this->view->load("content");
$content->load("clienti_form","user/add");
$this->view->render();
}
}
You really should try and search more about it, and learn!
I could learn a lot of the basics of CodeIgniter, watching this channel that has great content, and explains every detail: https://www.youtube.com/playlist?list=PLillGF-RfqbaP_71rOyChhjeK1swokUIS
function add_user($data) {
$this->db->insert("user",$data);
$ins_id =$this->db->insert_id();
return $ins_id;
}
use this in model..
and in controller set rules for each like this
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
// for all other
I am trying to update a mySQL table while using CodeIgniter.
The Controller
I have the redirect() commented out to use print_r(). When it returns the array, it returns the correct updated values. But if i uncomment the redirect, then it will redirect me to the page with the table and the values will not get updated. I also check on phpMyAdmin to make sure values arent getting updated and not just displaying, but they arent updating on their either. This is confusing me because the print_r() is returning the correct values.
<?php
class update_ctrl extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('update_model');
}
public function updateGame($id){
$this->load->model('update_model');
$data['games'] = $this->update_model->getGame($id);
$this->load->view('update_view', $data);
$this->load->view('footer');
}
public function update(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('genre', 'Genre', 'trim|required');
$this->form_validation->set_rules('developer', 'Developer', 'trim|required');
$this->form_validation->set_rules('year', 'YearReleased', 'trim|required|numeric');
$this->form_validation->set_rules('price', 'Price', 'trim|required|numeric');
$id = $this->input->post('ID');
$data = array(
'Name' => $this->input->post('name'),
'Genre' => $this->input->post('genre'),
'Developer' => $this->input->post('developer'),
'YearReleased' => $this->input->post('year'),
'Price' => $this->input->post('price')
);
$this->load->model('update_model');
$this->update_model->update($id, $data);
$this->session->set_flashdata('msg', 'Game Updated!');
print_r($data);
//redirect('');
}
}
?>
The Model
<?php
class update_model extends CI_Model {
public function getGame($id) {
$this->db->select('*');
$this->db->from('games');
$this->db->where('ID', $id);
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
} else {
return $query->result();
}
}
public function update($id, $data){
$this->db->where('ID', $id);
$this->db->update('games', $data);
}
}
?>
The only thing I can think of is that your query is silently failing. Go into database.php and change db_debug to TRUE and run everything again.
I would also like to add that you seem to do a good job making sure all your fields are validated. I'd also add a validation for id just to make sure you are getting it - in the case the user does something funky then they won't get some weird query error because the id wasn't set.
Comment out your all $this->form_validation->set_rules and try again, I think the numeric validation get errors
public function update(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
//$this->form_validation->set_rules('name', 'Name', 'trim|required');
//$this->form_validation->set_rules('genre', 'Genre', 'trim|required');
//$this->form_validation->set_rules('developer', 'Developer', 'trim|required');
//$this->form_validation->set_rules('year', 'YearReleased', 'trim|required|numeric');
//$this->form_validation->set_rules('price', 'Price', 'trim|required|numeric');
$id = $this->input->post('ID');
$data = array(
'Name' => $this->input->post('name'),
'Genre' => $this->input->post('genre'),
'Developer' => $this->input->post('developer'),
'YearReleased' => $this->input->post('year'),
'Price' => $this->input->post('price')
);
$this->load->model('update_model');
$this->update_model->update($id, $data);
$this->session->set_flashdata('msg', 'Game Updated!');
print_r($data);
//redirect('');
}
}
I need to redirect the url with hashtag in Codeigniter3. There is no error but its not redirect with hashtag. Please help me to fix it.
http://localhost:8888/CodeIgniter/index/aboutus#aboutusredirect
Controller.
public function aboutus()
{
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('include/header.php');
$this->load->view('about');
$this->load->view('include/footer.php');
}
else
{
$this->load->view('profilecreate');
}
}
Inside the if condition change
$this->load->view('include/header.php');
$this->load->view('about');
$this->load->view('include/footer.php');
use redirect function like below:
redirect('index/aboutus#aboutusredirect');
you can use required_once() function in 'about' view and then use redirect() for go another page.
you change about view like this :
required_once('include/header.php')
//html tag in about view
required_once('include/footer.php')
and then you're code must be like this :
public function aboutus()
{
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('about');
}
else
{
$this->load->view('profilecreate');
}
}
Anyone know about form validation in CI here ?
As long this script in my function, it will give error
I dont know why, I just copy paste from the user guide, and put it in my function
and also, I cant do autoload.php -> $autoload['libraries'] = array('database', 'session');
and this is the code for form validation :
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('item_name', 'Item Name', 'is_numeric|required');
$this->form_validation->set_rules('item_price', 'Item Price', 'required');
$this->form_validation->set_rules('item_description', 'Item Description', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->create();
}else{
$this->load->view('formsuccess');
}
please help me guys..
first error of form validation
second error of form validation
complete code :
function submit(){
$this->load->library('form_validation');
$this->load->helper('form', 'url');
$this->form_validation->set_rules('item_name', 'Item Name', 'is_numeric|required');
$this->form_validation->set_rules('item_price', 'Item Price', 'required');
$this->form_validation->set_rules('item_description', 'Item Description', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->create();
}
else
{
$this->load->view('formsuccess');
}
}
Try This:
$this->load->model('login_database','file');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('item_name', 'Item Name', 'is_numeric|required');
$this->form_validation->set_rules('item_price', 'Item Price', 'required');
$this->form_validation->set_rules('item_description', 'Item Description', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('formsuccess');
}else
{
$this->create();
}
when you using HMVC
dont forget to use MX_controller.
This is the full solution of my problem :
Looks like your using HMVC Please show the full template.php file not if controller file should be upper case for first letter of class name and file name example Template.php and class Template extends MX_Controller{}
I have create a form with 2 file upload fields and basically I want to make them required fields. I created a call back function but just can't seem to make it work properly. It is using the callback and posts the error if some other fields are left blank but sends the form whether files are attached or not.
I'm pretty new to Codeigniter. Any help would be very much appreciated!
Controller:
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//Upload errors array
$up_errors = array();
$this->form_validation->set_rules('first_name', 'First Name', 'required|alpha');
$this->form_validation->set_rules('last_name', 'Surname', 'required|alpha');
$this->form_validation->set_rules('dob', 'Date of Birth', 'required');
$this->form_validation->set_rules('nationality', 'Nationality', 'required|alpha');
$this->form_validation->set_rules('gender', 'Gender', 'required');
$this->form_validation->set_rules('address_l1', 'Address Line 1', 'required|alpha_dash_space');
//$this->form_validation->set_rules('address_l2', 'Address Line 2', 'alpha');
$this->form_validation->set_rules('address_city', 'City', 'required|alpha');
$this->form_validation->set_rules('address_postcode', 'Post Code', 'required|alpha_dash_space');
//$this->form_validation->set_rules('address_country', 'Country', 'required|alpha_dash_space');
$this->form_validation->set_rules('e_address', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('h_tel', 'Home Telephone Number', 'required|numeric');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required|numeric');
$this->form_validation->set_rules('university', 'University', 'required|alpha_dash_space');
$this->form_validation->set_rules('campus', 'Campus Name', 'required|alpha_dash_space');
$this->form_validation->set_rules('course', 'Course Title', 'required|alpha_dash_space');
$this->form_validation->set_rules('end_date', 'Course End Date', 'required');
//Custom callback
$this->form_validation->set_rules('file', 'Attachment', 'callback_handle_upload');
//Check if file attached
//if (isset($_FILES['file']))
//{
//}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('home');
}
else
{
//Display Success page
$this->load->view('formsuccess');
//Array helper
$this->load->helper('array');
//Set form data array
$fields = $this->input->post('first_name')."\n".
$this->input->post('last_name')."\n".
$this->input->post('dob')."\n".
$this->input->post('nationality')."\n".
$this->input->post('gender')."\n".
$this->input->post('address_l1')."\n".
$this->input->post('address_l2')."\n".
$this->input->post('address_city')."\n".
$this->input->post('address_postcode')."\n".
$this->input->post('address_country')."\n".
$this->input->post('e_address')."\n".
$this->input->post('h_tel')."\n".
$this->input->post('mobile')."\n".
$this->input->post('university')."\n".
$this->input->post('campus')."\n".
$this->input->post('course')."\n".
$this->input->post('end_date');
$msg = serialize($fields);
//Upload files to server
$this->load->library('upload');
//Send Email
$this->load->library('email');
//Set config
$config['upload_path'] = './attachments'; //if the files does not exist it'll be created
$config['allowed_types'] = 'gif|jpg|png|doc|docx|txt|pdf';
$config['max_size'] = '4000'; //size in kilobytes
$config['encrypt_name'] = TRUE;
$this->upload->initialize($config);
$uploaded = $this->upload->up(FALSE); //Pass true if you want to create the index.php files
//Attach the 2 files to email
foreach($_FILES as $key => $value)
{
//var_dump($uploaded['success'][$key]['full_path']); //FOR TESTING
$file = $uploaded['success'][$key]['full_path'];
$this->email->attach($file);
//unlink($file);
}
//var_dump($msg); //FOR TESTING
$this->email->from($this->input->post('e_address'),$this->input->post('first_name') . $this->input->post('last_name'));
$this->email->to('tom#shu.ac.uk');
$this->email->subject('NON-SHU STUDENT REQUEST');
$this->email->message($msg);
$this->email->send();
//echo $this->email->print_debugger();
}
}
function alpha_dash_space($str_in)
{
if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in)) {
$this->form_validation->set_message('_alpha_dash_space', 'The %s field may only contain alpha-numeric characters, spaces, underscores, and dashes.');
return FALSE;
} else {
return TRUE;
}
}
function handle_upload()
{
if (count($_FILES['file']['name'] < 2)
{
// throw an error because nothing was uploaded
$this->form_validation->set_message('handle_upload', "You must attach both files!");
return false;
}
else
{
return TRUE;
}
}
}
?>
check the following form validation extension which can help you to validate files, images with CI form validation library copy the code and create MY_Form_validation.php file in application library and paste code in that file and save
form file validation
code to copy