Skip to content

Instantly share code, notes, and snippets.

@gitbejbe
Last active December 24, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitbejbe/6762020 to your computer and use it in GitHub Desktop.
Save gitbejbe/6762020 to your computer and use it in GitHub Desktop.
Klasa walidacji
<?php
abstract class Validation_engine
{
protected $key, $value; // BUFOR
public $input_data = array(); // Przechowuje wprowadzone dane do walidacji. Wywołuje sie je poprzez klucz
public $data = array(); // data to validate -> array(x) {["KEY"]=> array(1) {["RULES"] => "TEXT TO VALIDATE"}}
public $errors = array(); // errors array
public function addValue($value) //mozna zrobic jako __construct, ale wtedy lipa z funkcja getValues bo jesli zmienne POST/GET nie istnieje to bedzie blad
{
$this->value = $value; //add to bufor
$this->input_data[$this->key] = $value; //przechowaj dane dla metody getValue
return $this; //return object
}
public function addKey($key)
{
$this->key = $key; //add to bufor
return $this; //return object
}
public function getValue($key)
{
if(array_key_exists($key, $this->input_data))
{
echo htmlspecialchars($this->input_data[$key]);
}
else {return false;}
}
public function getErrors($error_key)
{
if(array_key_exists($error_key, $this->errors))
{
echo $this->errors[$error_key];
}
else {return false;}
}
public function __call($name, $arguments) //jesli wpisze się błędną nazwe walidatora
{
if(!empty($arguments)){ echo 'Metoda "'.$name.' ('.implode(', ', $arguments).')" nie istnieje'; }
else echo 'Metoda "'.$name.'" nie istnieje';
exit;
}
protected function addError($key, $error_message)
{
if(!array_key_exists($key, $this->errors)) //kolejnosc podanych walidatorów odpowiada kolejności podawanych błędów
{
$this->errors[$key] = $error_message;
}
}
}
/************************************************
VALIDATION RULES
************************************************/
class Validation extends Validation_engine
{
public function required()
{
if(empty($this->value)) { $this->addError($this->key, 'te pole musi być uzupełnione'); }
return $this;
}
public function min($lenght)
{
if(strlen($this->value) < $lenght) { $this->addError($this->key, 'to pole musi posiadać minimum <b>'.$lenght.'</b> litery');}
return $this;
}
public function max($lenght)
{
if(strlen($this->value) > $lenght) { $this->addError($this->key, 'to pole musi posiadać maksymalnie <b>'.$lenght.'</b> litery');}
return $this;
}
public function is_int()
{
if(!is_numeric($this->value)) { $this->addError($this->key, 'Tylko cyfry !');}
return $this;
}
public function login()
{
if(!preg_match("/^[a-zA-Z1-9_]+$/", $this->value))
{
$this->addError($this->key, 'to pole może moze posiadać tylko litery i cyfry oraz znak "_"');
}
return $this;
}
public function url()
{
if (!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $this->value))
{
$this->addError($this->key, 'błędny URL');
}
return $this;
}
}
?>
<?php
$val = new Validation();
if(isset($_POST['zarejestruj']))
{
$val -> addKey('imie') -> addValue($_POST['imie']) -> required() -> min(5) -> max(10) ->is_int();
$val -> addKey('login') -> addValue($_POST['login']) -> required() -> min(5) -> login() -> url();
}
?>
<br><br>
<form method="POST" action="test.php">
imie: <input type="text" name="imie" value="<?php $val->getValue('imie'); ?>"><?php $val->getErrors('imie'); ?><br>
login: <input type="text" name="login" value="<?php $val->getValue('login'); ?>"><?php $val->getErrors('login'); ?><br>
email: <input type="text" name="email"><?php $val->getErrors('email'); ?><br>
haslo: <input type="text" name="haslo"><br>
haslo2: <input type="text" name="haslo2"><br>
IP: <input type="text" name="ip"><br>
zapisz: <input type="submit" name="zarejestruj"><br>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment