Welcome Guest, Not a member yet? Register   Sign In
Data validation/sanitation library
#1

[eluser]sorenchr[/eluser]
EDIT: I fixed some misinformation in the documentation.

This is a small albeit useful library I created for validating/sanitizing data. I use it primarily to make sure my data is clean before I throw it in a database.

Using this library is much like using CI's native Form validation library, however it doesn't serve the same purpose. Here's a basic example of how to use it:

Code:
$somedata = "some data we want checked and sanitized";

//run data through the validator
$output = $this->validation->validate($somedata, 'max_length[255]|valid_email|trim|cut[0,10]');

//did the data validate?
if($output == FALSE)
{
  echo "data is invalid";
}
else
{
  echo "the validated and sanitized data is: ".$output;
}

You can also validate/sanitize an array of data and rules, but it has to be a multidimensional associative array, like so:

Code:
$array = array('string1' => array('somedata' => 'trim|valid_email'),
               'string2' => array('somedata' => 'alpha_numeric'));

$output = $this->validation->validate($array);

//did the data validate?
if($output == FALSE)
{
  echo "data is invalid";
}
else
{
  // here the output would then be the sanitized and validated array.
  print_r($output);
}

The library basically iterates over the rules you've provided, as many as you like of course, creating new rules for validation/sanitation is simple and easy. It then returns the sanitized data, if the data validated. Check the file for all available validation/sanitation methods. The validation function returns FALSE if the data did not validate.

As always, make sure you look through the code before using it. I've checked it myself, but that's not a bug-free guarentee Smile
Place the file in your application/libraries folder and you are set.
#2

[eluser]Ochetski[/eluser]
It joins all validation methods from CI validation library in a unique method?
#3

[eluser]sorenchr[/eluser]
[quote author="Ochetski" date="1292309166"]It joins all validation methods from CI validation library in a unique method?[/quote]

No, the methods are declared in the library itself. Nothing is used from CI's system.
#4

[eluser]Ochetski[/eluser]
Hmmm... nice work, guessed it was another kind of code. %-P
#5

[eluser]n0xie[/eluser]
I'm curious why would one use this instead of the basic form_validation class? What are the pros/cons ?
#6

[eluser]sorenchr[/eluser]
@noxie Because the form_validation class doesn't act as a validation tool for simple data. It's purpose is to validate form data. With this library, you can validate data that have nothing to do with forms.
#7

[eluser]Unknown[/eluser]
I was unable to download the attachment, can someone re-post it? Many thanks
#8

[eluser]alexwenzel[/eluser]
Nice contribution. Always wanted that kind of validation library. Will test it on my next project.
#9

[eluser]alexwenzel[/eluser]
Btw. Can you push your library to bitbucket or github so we can fork this?




Theme © iAndrew 2016 - Forum software by © MyBB