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?
Related
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').']');
Here Is My COde I m checking username is already exist or not in datbase
when i validate and submit the form duplicate entry entered in database i want that if already exist it show validation error
My Controller
public function index()
{
if($this->input->post('submit')) {
$this->form_validation->set_rules('name', 'User Name', 'callback_checkuser');
$this->form_validation->set_rules('role', 'Role', 'trim|required');
$this->form_validation->set_rules('pass', 'Password', 'trim|required');
if($this->form_validation->run()==TRUE)
{
$user['u_name'] = $this->input->post('name');
$user['role'] = $this->input->post('role');
$user['password']= md5($this->input->post('pass'));
$u_id = $this->custom_model->add_user($user);
if($u_id){
$data['msg'] = 'Successfully Created!!!!';
}
}
}
$this->load->template('add_user', $data);
}
function checkuser($name) {
if($this->custom_model->check_name($name) == false) {
false;
}else {
$this->form_validation->set_message('checkuser', 'This user already exist');
return true;
}
}
Here is My Model
public function check_name($name) {
$sql = "SELECT * FROM users WHERE u_name='".$name."' ";
$query = $this->db->query($sql);
$res = $query->row_array();
if (is_array($res) && count($res) > 0){
return $res;
}
return false;
}
There is a return statement missing in the function checkuser, but more importantly, you should invert the value you return. According to the example in the docs, when you set a validation message because of a validation error, you should return false, and return true when the validation passes.
So add a return, and change the boolean values. BTW, you don't really need an else clause and the word "exist" needs an additional "s":
function checkuser($name) {
if ($this->custom_model->check_name($name) == false) {
return true; // the user does not yet exist, so all is OK
}
$this->form_validation->set_message('checkuser', 'This user already exists');
return false; // this is a duplicate user name: not OK
}
Use this:
$this->form_validation->set_rules('name', 'User Name', 'trim|required|is_unique[users.u_name]');// trim and required added too
Docs.
May I know why my insert function will display the validation message first even i do not insert any data yet.once I enter the insert page,it display the validation message.i have try to move the form validation to the below of the if else statement,it do not show the validation message but it cannot submit the data to the database. below is my controller function.
public function Insert_Result($Course_ID=null)
{
$this-load-helper('form');
$this-load-library('form_validation');
/* Model */
$this-load-model('ResultEvaluation');
/* Session */
$session_data = $this-session-userdata('logged_in');
$data['Ins_ID'] = $session_data['Ins_ID'];
$this-session-set_userdata($data);
/* Form Validation*/
//$this-form_validation-set_rules('Course_ID', 'Course_ID', 'required');
$this-form_validation-set_rules('Matric_No', 'Matric_No','required');
$this-form_validation-set_rules('Student_Name', 'Student_Name', 'required');
$this-form_validation-set_rules('Result_Mark_1', 'Result_Mark_1', 'required');
$this-form_validation-set_rules('Result_Mark_2', 'Result_Mark_2', 'required');
$this-form_validation-set_rules('Result_Mark_3', 'Result_Mark_3', 'required');
$this-form_validation-set_rules('Result_Mark_4', 'Result_Mark_4', 'required');
$this-form_validation-set_rules('Result_Mark_5', 'Result_Mark_5', 'required');
if ($this-form_validation-run() === FALSE)
{
$data['results'] = $this-ResultEvaluation-get_record();
$data['query'] = $this-ResultEvaluation-view($Course_ID);
$this-load-view('templates/header');
$this-load-view('Insert_Result', $data);
$this-load-view('templates/footer');
}
else
{
$my_action = $this-input-post('submit');
if ($my_action == 'Submit')
{
$this-ResultEvaluation-insert_record($Course_ID);
redirect('Result_Evaluation/Student_Result_List/'.$Course_ID,
'refresh');
}
}
$my_action = $this-input-post('submit');
if ($my_action == 'Cancel')
{
redirect('Result_Evaluation/Student_Result_List/'.$Course_ID,
'refresh');
}
}
I think this should help
public function index(){
$this->load->library(array('form_validation'));
$this->form_validation->set_rules('field_name', 'Text','trim|required');
if($this->form_validation->run()==TRUE){
// do something
}else{
// validation error
redirect("to/your/controller");
break;
}
}
for more information visit : http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
I've this registration function and the related callback to check if an username already exists in the database. It works all perfectly, but I have problems with flashdata message
Here's the code:
/*
* Checks registration
*/
public function verify(){
$this->form_validation->set_rules('nome', 'Nome', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('cognome', 'Cognome', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|xss_clean|callback_check_username');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password_conf', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('error_reg', 'Something goes wrong, please check your registration form');
redirect('registration');
}
else
{
$this->registration_m->add_user();
$this->session->set_flashdata('success_reg', 'Registration Successful!');
redirect('registration');
}
}
public function check_username($username){
$result = $this->registration_m->check_username_m($username);
if ($result) {
$this->session->set_flashdata('username', 'Username già in uso, riprovare.');
return false;
} else {
return true;
}
}
As you can see there's a redirect on the function verify(), and the flashdata before is correctly shown in case of errors.
My Question is: is there a way to show (in case of error) the flashdata inside the callback function without changing the logic of this piece of code?
Hope I was clear at all, cheers.
Modify your code as below
public function check_username($username){
$result = $this->registration_m->check_username_m($username);
if ($result) {
$this->form_validation->set_message('check_username', 'The %s field can not be the empty');
return false;
} else {
return true;
}
}
See differences in code
$this->session->set_flashdata('username', 'Username già in uso, riprovare.');
$this->form_validation->set_message('check_username', 'The %s field can not be the empty');
I have a form that I submit with jQuery ajax and have it being sent to a controller function called submit to validate and do any other tasks I need to with the form data. I'm trying to find out why my form validation library isn't showing an error when the username doesn't contain only lowercase letters and numbers.
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');
POST Value after form submission:
username TestingUSER
EDIT:
As far as I know it gets to the php server side properly.
PHP:
public function submit()
{
$output_status = 'Notice';
$output_title = 'Not Processed';
$output_message = 'The request was unprocessed!';
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
$this->form_validation->set_rules('remember', 'Remember Me', 'trim|xss_clean|integer');
if ($this->form_validation->run() == TRUE)
{
}
else
{
$output_status = 'Error';
$output_title = 'Form Not Validated';
$output_message = validation_errors();
}
echo json_encode(array('output_status' => $output_status, 'output_title' => $output_title, 'output_message' => $output_message));
}
EDIT 2:
Based off of Sheikh answer. I am getting a response back that says "Unable to access an error message corresponding to your field name." It does say Form Not Validated for the title so the message isn't working.
public function check_username($str)
{
if (preg_match('#[0-9]#', $str) && preg_match('#[a-z]#', $str))
{
return TRUE;
}
$this->form_validation->set_message('username', 'This is not have an accepted value!');
return FALSE;
}
EDIT 3:
What I'm wanting to do is have it report back that there there are validation errors but not the specific errors in the pnotify response. However I do want it to display the specific errors under the form elements.
jQuery Code:
http://pastebin.com/1KehMJkh
Login Form:
http://pastebin.com/EfpBfbfN
I think you can use a callback function in your controller
public function check_username($str)
{
if (preg_match('#[a-z0-9]#', $str)) {
return TRUE;
}
$this->form_validation->set_message('check_username', 'This is not have an accepted value!');
return FALSE;
}
Validation rules for username
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_check_username');
You may like this too.