form validation CodeIgniter error - php

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{}

Related

Add user on Codeigniter 3

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

CI Error on libraries\Form_validation.php on line 450

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

Codeigniter redirect url with hashtag is not working

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');
}
}

callback not working in codeigniter 3.0

<?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('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confpassword', 'Password', 'required|matches[password]', 'callback__matcherror');
//$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
//$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else
{
$this->load->view('insert_dream');
}
}
public function _matcherror() {
$this->form_validation->set_message('_matcherror', 'Passwords should match');
return FALSE;
}
}
?>
i am a newbie to codeigniter. The above code doesnt display passwords should match error message. Is something wrong with the callback or Am i missing something.
Take a look here. You don't need to make a callback.
You are passing callback__matcherror as fourth parameter of set_rules function.It should be 3rd parameter. Use this way
$this->form_validation->set_rules('confpassword', 'Password', 'required|matches[password]|callback__matcherror');
Note
You will get this error message if your password fields match.Because you applying 3 rule there.3rd rule(call_back_function) will apply when 2nd rule is success. Your 2nd rule will valid when passwords matches.
matches[password]
will automatically check for password. You need not to use callback function callback__matcherror

Codeigniter - input type file as required field

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

Categories