CodeIgniter Forums

Full Version: Validation with variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi

I am completely lost. I with to use CI Validation, but I am not using the Forms functionality.

I just need to validate a variable, but I don't know how to do that using CI. I guess it is possible, and I hope also.

Any tip for me?
Hello ruiganga ,

It would be nice if you could show some codes and and further explain what kind of validation you need.
The situation is this.

An AJAX call sends an email using POST. I need to check if the email is valid. thats it.

Because CI has the Validation Library my question is: is it possible to use that library to validate variables like this?
from your Ajax to CI. I think its possible, you can pass $_POST variable to any class (controller) in CI. you'll need to use some callback function. please check http://www.codeigniter.com/userguide3/li...on-methods or read entire http://www.codeigniter.com/userguide3/li...ation.html cause i think you'll need to use the form_validation lib in order to perform callback functions. 
Since you do a POST ajax call, the variables you send in that call will be available in the $_POST global. This means you can use the CodeIgniter form validation library just like you wold for a regular submitted form. I use it all the time.

Lets say the variable you send in your AJAX call is 'user_email'. This is what I would do to validate it.

PHP Code:
// Load library here or in your autoload config file
$this->load->library('form_validation');

// Set up validation rules
$this->form_validation->set_rules('user_email''Users email''required|valid_email');

if (
$this->form_validation->run() == FALSE)
{
 
   // Submitted data not valid
}
else
{
 
   // Submitted data is valid

(04-04-2015, 11:02 AM)silentium Wrote: [ -> ]Since you do a POST ajax call, the variables you send in that call will be available in the $_POST global. This means you can use the CodeIgniter form validation library just like you wold for a regular submitted form. I use it all the time.

Lets say the variable you send in your AJAX call is 'user_email'. This is what I would do to validate it.


PHP Code:
// Load library here or in your autoload config file
$this->load->library('form_validation');

// Set up validation rules
$this->form_validation->set_rules('user_email''Users email''required|valid_email');

if (
$this->form_validation->run() == FALSE)
{
 
   // Submitted data not valid
}
else
{
 
   // Submitted data is valid

This way doesn't work. Data is neve valid. Always that is NOT valid.

$this->form_validation->set_rules($email , 'Users email', 'required|valid_email');

if ($this->form_validation->run() == FALSE)
{
echo " Submitted data IS NOT valid";
}
else
{
echo " Submitted data IS valid";
}

$email is the variable where I put the valid email to test. Am I doing something wrong?
I've found this solution:

// validate data
$this->load->library('form_validation');
$validation_status = 1; // by default validation is ok
$fields_failed = array();

// EMAIL
if(!$this->form_validation->valid_email($email)){ //if email is not valid
$validation_status = 0;
array_push($fields_failed, "email");
}

if($validation_status){//no error in data, continue
$answer -> status = "1";
$answer -> message = "Tutti-frutti, validation is OK we can continue...";
} else {
$answer -> fields_failed = $fields_failed;
$answer -> status = "0";
$answer -> message = "Some data failed validation!";
}

print_r($answer);
exit;

Is it possible to prevent XSS and other security issues using this same approach, or, with 1 line "treat" all post data?
Awsome. Even is_unique works like this!!

if(!$this->form_validation->is_unique($email, "tablename.fieldname")){ //if email already exists
$validation_status = 0;
array_push($fields_failed, "email");
}
(04-04-2015, 04:31 PM)ruiganga Wrote: [ -> ]This way doesn't work. Data is neve valid. Always that is NOT valid.

$this->form_validation->set_rules($email , 'Users email', 'required|valid_email');

if ($this->form_validation->run() == FALSE)
{
  echo " Submitted data IS NOT valid";
}
else
{
   echo " Submitted data IS valid";
}

$email is the variable where I put the valid email to test. Am I doing something wrong?

Yes, you are.
The form validation do not take variables like that. The first value, where you have $email, is the name of the POST data in global $_POST array. As in my example, lets say that in the AJAX request you do, you send a parameter called 'user_email'. In the controller you send the request to, that 'user_email' data will be available in the global $_POST array as $_POST['user_email']

So, in $this->form_validation->set_rules(); you set the first option to 'user_email' as that is the name of the your data in the global $_POST array.

If you for some reason don't have the data available in the global $_POST array anymore, then you can validate any other type of array. So, in your case, you could create a new array with your email and then set that as the array that form validation should validate.

Here is an example.
PHP Code:
$validate_data = array('email' => $email);

$this->form_validation->set_data($validate_data);
$this->form_validation->set_rules('email' 'Users email''required|valid_email');
 
 if (
$this->form_validation->run() == FALSE)
 {
 
  echo " Submitted data IS NOT valid";
 }
 else
 {
 
   echo " Submitted data IS valid";
 } 

More info about validation an array here http://www.codeigniter.com/userguide3/li...-than-post
Pages: 1 2