CodeIgniter Forums
Validation with variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Validation with variables (/thread-61253.html)

Pages: 1 2


Validation with variables - ruiganga - 04-04-2015

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?


RE: Validation with variables - vrsotto - 04-04-2015

Hello ruiganga ,

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


RE: Validation with variables - ruiganga - 04-04-2015

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?


RE: Validation with variables - vrsotto - 04-04-2015

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/libraries/form_validation.html#callbacks-your-own-validation-methods or read entire http://www.codeigniter.com/userguide3/libraries/form_validation.html cause i think you'll need to use the form_validation lib in order to perform callback functions. 


RE: Validation with variables - silentium - 04-04-2015

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




RE: Validation with variables - ruiganga - 04-04-2015

(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




RE: Validation with variables - ruiganga - 04-04-2015

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?


RE: Validation with variables - ruiganga - 04-04-2015

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?


RE: Validation with variables - ruiganga - 04-04-2015

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


RE: Validation with variables - silentium - 04-06-2015

(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/libraries/form_validation.html#validating-an-array-other-than-post