codeigniter form validation doesn't call callback function - php

By some reason form validation doesn't call callback function which I set in the rules.
It is rules sets
if( ! empty($_POST))
{
ci()->form_validation->set_rules('login', 'Username', 'trim|required');
ci()->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_check_email');
if (ci()->form_validation->run() == TRUE)
{
}
}
and a function
public function check_email($str)
{
ci()->load->model(array('secure_model', 'admin/members_model'));
$o['username'] = ci()->input->post('login');
$o['email'] = ci()->input->post('email');
$m = $this->_model->get_row($o);
if ( ! $m)
{
$this->form_validation->set_message('check_email', lang('unlock_incorrect_login'));
return FALSE;
}
else
{
return FALSE;
}
}
I set FALSE twice to see if it call callback function but it doesn't display error message. So I suppose CI doesn't try to call it. What is wrong?

In your check_email() function, you're using $this->form_validation instead of ci()->. I'm not sure why you're using ci() instead of the standard CI way of using $this but that's the part in your code which seems off compared to the rest.

Related

form validation with preg_match in callback not working

I have questions about my codeigniter form validation. I try to validate the input form for name so it will generate error if user using symbol like ">?<*&%^$". Here is my code:
My rules:
$this->load->library('form_validation');
$this->form_validation->set_rules('full_name', 'Name', 'trim|required|callback_name_check',
array(
'name_check' => '%s should not using symbols'
)
);
This is my callback function (I tried to modify this from the last example I saw, so I thought the problem was here)
public function password_check($str)
{
if (preg_match('#[<>?&%$##]#', $str)) {
return TRUE;
}
return FALSE;
}
I have tried another example from another StackOverflow answer to use / as delimiter (like this --> [/<>?&%$##/]), but still, doesn't work. I'll appreciate your help sensei :)
Validation should be
$this->load->library('form_validation');
$this->form_validation->set_rules('full_name', 'Name', 'trim|required|callback_name_check');
Inside call back function
public function name_check($str)
{
if (preg_match('#[<>?&%$##]#', $str)) {
{
return TRUE;
}
else
{
#adding new validation error should be
$this->form_validation->set_message('full_name', '%s should not using symbols'); # input field name should come to first
return FALSE;
}
}
Note: Didn't validate REGEX which you have posted

Can't set error message in Code Igniter 3 when using callback function

I am writing a method that uses POST variables posted by AJAX to add a user to a certain course in the database, but I can't get the callback to work correctly:
public function enroll()
{
$package = array();
$this->load->library('form_validation');
$this->form_validation->set_rules('course', 'Vak', 'required|callback_not_enrolled');
$fields = array("course");
if ($this->form_validation->run($this) === FALSE) {
$errors = array();
$success = array();
foreach ($fields as $field) {
$error = form_error($field);
if ($error !== "") {
$errors[$field] = $error;
} else {
$success[$field] = True;
}
}
$package["field_errors"] = $errors;
$package["field_success"] = $success;
$package["success"] = False;
} else {
$package["database"] = $this->course_model->enroll_user($this->data["user"], $this->input->post("course"));
$package["success"] = True;
}
echo json_encode($package);
}
I wrote the callback not_enrolled to check if the user is not already enrolled to the database. Note that I can't use is_unique because I have to test the combined uniqueness of two fields (so just one or two separate ones don't do the trick) and the id of the user is not included in the form (because it's part of the Code Igniter session).
The callback function:
public function _not_enrolled($course)
{
$exists = ($this->user->is_enrolled($course, $this->data["user_id"]) != False);
if ($exists != False) {
$this->form_validation->set_message("not_enrolled", "Already enrolled");
return False;
} else {
return True;
}
}
And finally the method is_enrolled from the model:
public function is_enrolled($course, $user=False) {
if($user==False){
$user = $this->data["user_id"];
}
$this->db->select()->from("course_participant")->where("user_id", $user)->where("course_id", $course);
$query = $this->db->get();
return($query->num_rows()>0);
}
Through a call to var_dump($this->_not_enrolled($existing_course_id)); I know that both the callback function and the method from the model work, as it correctly returned true.
When I var_dump the $package array or validation_errors() I don't get any validation errors except that it says Unable to access an error message corresponding to your field name Vak(not_enrolled).
I tried removing the initial _ from the function name but that gives me a Server Status 500 error.
I have another setup exactly like this, albeit other database calls, with a callback using the same syntax. This method works perfectly.

GROCERY CRUD: set_rules callbacks not working

I tried to validate the password field using a callback function. But when I use the following code, all validations do not work any more WHEN I TYPE 1234 (callback condition). When I remove the validation which contains the callback function, other validations work perfectly..
This is my validation rules
$crud->set_rules('password', 'Password', 'callback_valid_password');
$crud->set_rules('confirm_password', 'Password Confirmation', 'required|matches[password]');
$crud->set_rules('email', 'Email', 'trim|required|valid_email');
Here is my callback function
function valid_password($str) {
if ($str=="1234")
{
$crud->set_message('_valid_password', 'The field should be 1234');
//do some pw validation
return FALSE;
}
else
{
return TRUE;
}
}
Please help me to find what is wrong here.. Thank you in advance
p.s - I am using php 5.4 version with the latest grocery crud version
function valid_password($str) {
if ($str=="1234")
{
$this->form_validation->set_message('valid_password', 'The field should be 1234');
//do some pw validation
return FALSE;
}
else
{
return TRUE;
}
}
For those who are still struggling to find the solution, please follow the checklist.
Are you using CodeIgniter as MVC or HMVC?
1. HMVC
(A) - Check if you have updated the file (./application/libraries/Grocery_crud.php) as suggested below.
(B) - Before "__construct" inside " "class Grocery_CRUD extends grocery_CRUD_States" add "protected $hmvc;"
(C) - Update "__construct" with as below:
public function __construct($hmvc = null)
{
$this->hmvc = $hmvc;
}
(D) - Update "form_validation" with as below:
protected function form_validation()
{
if ($this->form_validation === null) {
$this->form_validation = new grocery_CRUD_Form_validation();
if ($this->hmvc) $this->form_validation->CI = $this->hmvc;
$ci = &get_instance();
$ci->load->library('form_validation');
$ci->form_validation = $this->form_validation;
}
return $this->form_validation;
}
(E) - Use "$crud = new Grocery_crud($this);" instead "$crud = new Grocery_crud();" in your Controller.
(F) - GC set_rules example:
$crud->set_rules("level_title", 'Level Title Label', 'trim|required|callback_unique_level_field_check');
(G) - Callback method example:
public function unique_level_field_check ($level_title)
{
if ( empty($level_title))
{
$this->form_validation->set_message('unique_level_field_check', "Level Title Label should be unique");
return FALSE;
}
return TRUE;
}
2. MVC
Follow F & G only (above).
GroceryCRUD Forum: See details here

Nested if, not exiting - create a function to call functions

I have the following code to validate form data. I have created functions to validate various groups, and then have an if isset statement to check if these functions return true. I have tried many different ways to get this to work.
The problem I am having is this. I want the if isset to end if returning FALSE; but it doesn't, it keeps going and pops up the next alert (in my code I have many functions). How can I get it to exit after the first return FALSE? Do I need to make the isset into a function? So it can exit on return FALSE. thanks
I am having trouble writing a function to call functions in php.
function namecheck ($fname, $lname)
{
$regexp ="/^[A-Za-z]+$/";
//filter through names
if (preg_match($regexp,$fname,$lname))
{
return TRUE;
}
else
{
echo'<script type="text/javascript">alert("Enter your names.")</script>';
return FALSE;
}
}
function emailcheck ($email1, $email2)
{
$regexp="/^[a-zA-A-Z0-9_.]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9.-]+$/";
//validate email address
if (preg_match($regexp,$email1,$email2))
{
return TRUE;
}
else
{
echo '<script type="text/javascript">alert ("Enter a valid email address.")</script>';
return FALSE;
}
}
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$namecheck=namecheck($fname,$lname);
$email1=$_POST['email1'];
$email2=$_POST['email2'];
$emailcheck=emailcheck($email1,$email2);
if (isset($_POST['submit']))
{
if ($namecheck !==TRUE)
{
return FALSE;
}
elseif ($emailcheck!==TRUE)
{
return FALSE;
} //and so on..
else
{
return TRUE;
}
}
A general structure for your functions you could follow is something like this:
function validateName($name) {
// Do Validation. Return true or false.
}
function validateEmail($email) {
// Do Validation. Return true or false.
}
function isFormValid()
{
// Name Validation
if( ! validateName( $_POST['name'] ) )
return false;
// Email Validation
if( ! validateEmail( $_POST['email'] ) )
return false;
// Form is valid if it reached this far.
return true;
}
// In your regular code on Form Submit
if( isset($_POST['submit']) )
{
if( isFormValid() ) {
// Save Form Data to DB
} else {
// Show Some Errors
}
}
That general structure should work fine for you. It could be made a LOT better but, for the sake of learning, this is sufficient.
If you want the script to, as you put, "exit" then you need to use exit(); Generally this is bad as the script will completely stop executing. Maybe you can look into using "break;" to get you out of a loop and stop executing functions within that loop. Another problem is that you are echoing out HTML code in your function which gets executed on assignment and so you will always get an alert generated when it evaluates to FALSE.
edit:
within your if(isset()) block. Inside here you can do{}while(false); which is a loop and will let you break out of it at anytime and prevent further execution of code within that loop.
If a function isn't returning false, then it never reached a return FALSE; statement. It's as simple as that. So let's examine the relevant code:
if (isset($_POST['submit']))
{
if ($namecheck !==TRUE)
{
return FALSE;
}
elseif ($emailcheck !== TRUE)
{
return FALSE;
} //and so on..
else
{
return TRUE;
}
}
So, if $_POST['submit'] is set and is not null, the if block will be reached. Then, if $namecheck is not true OR $emailcheck is not true, the function will return FALSE. You can simplify the above code to just:
if (isset($_POST['submit']))
{
return !(!$namecheck || !$emailcheck);
}
However, it doesn't look like this code is inside a function, so the return statement will do nothing. You have to put it in a function if you want it to work like a function.
Beyond that, I can't help you. I don't know what you want to do with this code. You seem to know how to use and call functions, so I'm not sure what the problem is. If you want to return from a function, put code in a function and call return. Right now your code is not in a function, so return won't do anything.

codeigniter form callback value

Im carrying out some form validation with codeigniter using a custom validation callback.
$this->form_validation->set_rules('testPost', 'test', 'callback_myTest');
The callback runs in a model and works as expected if the return value is TRUE or FALSE. However the docs also say you can return a string of your choice.
For example if I have a date which is validated, but then in the same function the format of the date is changed how would I return and retrieve this new formatted value back in my controller?
Thanks for reading and appreiate the help.
I'm not entirely sure I got what you were asking, but here's an attempt.
You could define a function within the constructor that serves as the callback, and from within that function use your model. Something like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Controllername extends CI_Controller {
private $processedValue;
public function index()
{
$this->form_validation->set_rules('testpost','test','callback');
if ($this->form_validation->run()) {
//validation successful
echo $this->processedValue; //outputs the value returned by the model
} else {
//validation failed
}
}
private function callback($input)
{
$this->load->model('yourmodel');
$return = $this->yourmodel->doStuff($input);
//now you have the user's input in $input
// and the returned value in $return
//do some checks and return true/false
$this->processedValue = $return;
}
}
public function myTest($data){ // as the callback made by "callback_myTest"
// Do your stuff here
if(condition failed)
{
$this->form_validation->set_message('myTest', "Your string message");
return false;
}
else
{
return true;
}
}
Please try this one.
I looked at function _execute in file Form_validation of codeigniter. It sets var $_field_data to the result of callback gets(If the result is not boolean). There is another function "set_value". Use it with the parameter which is name of your field e.g. set_value('testPost') and see if you can get the result.
The way Tank_Auth does this in a controller is like so
$this->form_validation->set_rules('login', 'Login', 'trim|required|xss_clean');
if ($this->form_validation->run()) {
// validation ok
$this->form_validation->set_value('login')
}
Using the set_value method of form_validation is undocumented however I believe this is how they get the processed value of login after it has been trimmed and cleaned.
I don't really like the idea of having to setup a new variable to store this value directly from the custom validation function.
edit: sorry, misunderstood the question. Use a custom callback, perhaps. Or use the php $_POST collection (skipping codeigniter)...apologies haven't tested, but I hope someone can build on this...
eg:
function _is_startdate_first($str)
{
$str= do something to $str;
or
$_POST['myinput'} = do something to $str;
}
================
This is how I rename my custom callbacks:
$this->form_validation->set_message('_is_startdate_first', 'The start date must be first');
.....
Separately, here's the callback function:
function _is_startdate_first($str)
{
$startdate = new DateTime($this->input->post('startdate'), new DateTimeZone($this->tank_auth->timezone()));
$enddate = new DateTime($this->input->post('enddate'), new DateTimeZone($this->tank_auth->timezone()));
if ($startdate>$enddate) {
return false;
} else {
return true;
}
}

Categories