Codeigniter form_validation help - php

Hi there I am having a problem with my form validation, basically the problem is that I am getting a repeated view loaded if the validation fails, please see my code snippet,
else {
//the user is a new customer so we need to show them
//the card details form
$this->addViewData('returningCustomer', false);
$this->load->library('form_validation');
if($this->input->post('carddetails') == 'Submit') {
$this->form_validation->set_rules('inpcardtype', 'Card type', 'cardTypeCheck'); //custom callback
$this->form_validation->set_rules('inpcardnumber', 'Card number', 'required|trim|exact_length[16]');
$this->form_validation->set_rules('startmonth', 'Start month', 'trim|required');
$this->form_validation->set_rules('startyear', 'Start year', 'trim|required');
$this->form_validation->set_rules('endmonth', 'End month', 'trim|required');
$this->form_validation->set_rules('endyear', 'End year', 'trim|required');
$this->form_validation->set_rules('inpissuenumber', 'Issue number', 'trim|integer');
$this->form_validation->set_rules('inpccv', 'CCV Code', 'required|trim|exact_lenght[3]');
if($this->form_validation->run() == FALSE) {
$this->renderTemplate('cv/search/pf_shortlist.php', $this->viewData);
} else {
// validation is passed we need to safe the user details
// it is probable that we will need to hash the users card details
//#TODO: HASH USER CARD DETAILS LEAVE ONLY THE FINAL DIGITS
$this->load->model('checkout_model');
}
}
$this->renderTemplate('cv/search/pf_shortlist.php', $this->viewData);
basically if a condtion fails futher up the script a view is loaded with a form, the form is submitted and if validation fails the user is passed back to the form but is show the errors, however the original view is also present. Can I stop it doing this?

After
$this->renderTemplate('cv/search/pf_shortlist.php', $this->viewData);
You should do return; as after the else statement theres another view loading.#
//the user is a new customer so we need to show them
//the card details form
$this->addViewData('returningCustomer', false);
$this->load->library('form_validation');
if($this->input->post('carddetails') == 'Submit') {
$this->form_validation->set_rules('inpcardtype', 'Card type', 'cardTypeCheck'); //custom callback
$this->form_validation->set_rules('inpcardnumber', 'Card number', 'required|trim|exact_length[16]');
$this->form_validation->set_rules('startmonth', 'Start month', 'trim|required');
$this->form_validation->set_rules('startyear', 'Start year', 'trim|required');
$this->form_validation->set_rules('endmonth', 'End month', 'trim|required');
$this->form_validation->set_rules('endyear', 'End year', 'trim|required');
$this->form_validation->set_rules('inpissuenumber', 'Issue number', 'trim|integer');
$this->form_validation->set_rules('inpccv', 'CCV Code', 'required|trim|exact_lenght[3]');
if($this->form_validation->run() == FALSE) {
$this->renderTemplate('cv/search/pf_shortlist.php', $this->viewData);
return;// HERE
} else {
// validation is passed we need to safe the user details
// it is probable that we will need to hash the users card details
//#TODO: HASH USER CARD DETAILS LEAVE ONLY THE FINAL DIGITS
$this->load->model('checkout_model');
}
}
$this->renderTemplate('cv/search/pf_shortlist.php', $this->viewData);
return;

Related

Codeigniter how to use credit card helper

I have found a credit card validator. However, I am unsure of how to use it with callbacks. The function I want to use requires 4 inputs. However I can only pass one through the form validation now. Hope someone can help me with this.
Controller
public function next(){
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
$this->form_validation->set_rules('inputcardtype','Select Card Type','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Please select the month of expiration');
$this->form_validation->set_rules('inputcardnumber', 'Card Number', 'trim|required|xss_clean|callback_cardnumber_validation');
$this->form_validation->set_rules('inputexpirationdatemonth','Select Month','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Please select the month of expiration');
$this->form_validation->set_rules('inputexpirationdateyear','Select Year','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Please select the year of expiration');
$this->form_validation->set_rules('inputnameoncard', 'Name on Card', 'trim|required|xss_clean');
$inputcardnumber = $this->input->post('inputcardnumber');
$inputcardtype = $this->input->post('inputcardtype');
if($this->form_validation->run()==false){
$this->index();
}else{
}
}
function cardnumber_validation($string = NULL) {
$this->load->helper('creditcardvalidation');
if(checkCreditCard ($string, $cardtype, $ccerror, $ccerrortext)) {
return TRUE;
}
else{
$this->form_validation->set_message("cardnumber_validation", 'The %s is not valid.');
return FALSE;
}
}
The callbacks need to be named after a function.
If you have:
callback_check_default
You need to have a function called:
function check_default() {
//Your validation here
}
Does that answer your question?

CodeIgniter how to set validation message from function

I'm trying to validate credit card numbers and so far I am able to get a result from it. However, whenever it fails, the validation message for the form won't turn up. I've been trying several methods including setting a callback. I'm not sure what I'm missing. Hope someone can take a look and help me.
Controller
public function next(){
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
$this->form_validation->set_rules('inputcardtype','Select Card Type','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Please select the month of expiration');
$this->form_validation->set_rules('inputcardnumber', 'Card Number', 'trim|required|xss_clean');
$this->form_validation->set_rules('inputexpirationdatemonth','Select Month','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Please select the month of expiration');
$this->form_validation->set_rules('inputexpirationdateyear','Select Year','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Please select the year of expiration');
$this->form_validation->set_rules('inputnameoncard', 'Name on Card', 'trim|required|xss_clean');
$inputcardnumber = $this->input->post('inputcardnumber');
$inputcardtype = $this->input->post('inputcardtype');
// var_dump($this->cardnumber_validation($inputcardnumber,$inputcardtype));
if($this->form_validation->run()==false||$this->cardnumber_validation($inputcardnumber,$inputcardtype)==FALSE){
$this->index();
}else{
}
}
function cardnumber_validation($string = NULL,$cardtype=NULL) {
$this->load->helper('creditcardvalidation');
if(checkCreditCard ($string, $cardtype, $ccerror, $ccerrortext)) {
return TRUE;
}
else{
$this->form_validation->set_message("inputcardnumber", 'Invalid Card Number');
return FALSE;
}
}
function check_default($post_string){
return $post_string == '0' ? FALSE : TRUE;
}
So I found out you can do this to pass 2 variables into a callback
$this->form_validation->set_rules('inputcardnumber', 'Card Number', 'trim|required|xss_clean|callback_cardnumber_validation['.$this->input->post('inputcardtype').']');

form validation CodeIgniter error

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

Codeigniter Form Validation not submitting form and no errors

I had this form working and to be honest I can not figure out what I might have done. The form will not submit now and I don't see any form validation errors.
controller:
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="ncerror" >', '</div>');
$this->form_validation->set_rules('media_consent', 'Media Consent', 'required');
$this->form_validation->set_rules('aic_name', 'Name', 'required');
$this->form_validation->set_rules('aic_phone', 'Cell phone', 'required');
$this->form_validation->set_rules('aic_email', 'Email', 'required');
if ($this->form_validation->run() == FALSE) {
$data = $this->ncformdata();
$this->load->view('templates/sponsorheader', array('title' => 'National Convention Registration'));
var_export($_POST);
$this->load->view('ncform', $this->ncformdata());
$this->load->view('templates/footer2');
}
I have
<?php echo "errors" . validation_errors();
at the top of my form. After submitting form, the form reloads with "errors" displayed but no other output from the validation_errors function. Any help with troubleshooting?
should this line
$this->load->view('ncform', $this->ncformdata());
be this?
$this->load->view('ncform', $data);
You have a condition when
$this->form_validation->run() == FALSE
but I can not see any condition for when the form validation returns TRUE. Try adding a condition when
$this->form_validation->run() == TRUE
Example:
if ($this->form_validation->run() == FALSE) {
$data = $this->ncformdata();
$this->load->view('templates/sponsorheader', array('title' => 'National Convention Registration'));
var_export($_POST);
$this->load->view('ncform', $this->ncformdata());
$this->load->view('templates/footer2');
}
else
{
echo 'There are no form validation errors!.';
}

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