Ciao.
Penso che un approccio del
genere sia migliore anche se
sviluppato in maniera semplice (ne
esistono di molto + complessi vedi validator
class di Manuel Lemos (il mio idolo secondo
solamente a Alejandro Gervaso
Codice PHP:
<?php
abstract class validator
{
protected $_error;
abstract public function validate();
public function getErrors()
{
return $this->_error;
}
}
//Vlaiding Emails
class emailvalidator extends validator
{
private $_name;
private $_email;
public function __construct($name,$email)
{
$this->_name = $name;
$this->_email = $email;
}
public function validate()
{
$trimed_email = trim($this->_email);
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,8})$", $trimed_email))
{
$this->_error = $this->_name.' is not formated Correctly';
return false;
} else {
return true;
}
}
}
//Vlading for string to make sure they only include Text charchters
class textOnlyvalidator extends validator
{
private $_name;
private $_text;
public function __construct($name,$text)
{
$this->setName($name);
$this->setText($text);
}
private function setText($text)
{
$this->_text = $text;
}
private function setName($name)
{
$this->_name = $name;
}
public function validate()
{
$trimed = trim($this->_text);
if (preg_match("/\d/", $trimed))
{
$this->_error = $this->_name.' is not only text it includes numeric charcters';
return false;
} else {
return true;
}
}
}
//Vlading for numeric
class numericvalidator extends validator
{
private $_name;
private $_num;
public function __construct($name,$num)
{
$this->setName($name);
$this->setNum($num);
}
private function setName($name)
{
$this->_name = $name;
}
private function setNum($num)
{
$this->_num = $num;
}
public function validate()
{
$trimed = trim($this->_num);
if (!is_numeric($trimed))
{
$this->_error = $this->_name.' is not numeric';
return false;
}
else
{
return true;
}
}
}
class textLengthvalidator extends validator
{
private $_text;
private $_name;
private $_min = 0;
private $_max;
public function __construct($name,$text,$max,$min = 0)
{
$this->setName($name);
$this->setText($text);
$this->setMax($max);
$this->setMin($min);
}
private function setText($text)
{
$this->_text = $text;
}
private function setName($name)
{
$this->_name = $name;
}
private function setMax($max)
{
$this->_max = $max;
}
private function setMin($min)
{
$this->_min = $min;
}
public function validate()
{
$newtext = trim($this->_text);
$strlength = strlen($newtext);
if ($strlength > $this->_max)
{
$this->_error= $this->_name.' should only be '.$this->_max.' characters long';
return false;
} elseif ($strlength <= $this->_min) {
$this->_error = $this->_name.' should be atleast '.$this->_min.' characters long';
return false;
} else {
return true;
}
}
}
class phoneNumbervalidator extends validator
{
private $_number;
private $_name;
public function __construct($name,$num)
{
$this->setName($name);
$this->setNumber($num);
}
private function setName($name)
{
$this->_name = $name;
}
private function setNumber($num)
{
$this->_number = $num;
}
public function validate()
{
$trimed = trim($this->_number);
if (!(strlen($trimed) == 10) || !(is_numeric($trimed)))
{
$this->_error = $this->_name.' is incorrect';
} else {
return true;
}
}
}
$class[] = $email = new emailvalidator('User Email','asdfasdfs.com');
$class[] = $text = new textOnlyvalidator('User name','Vitaly');
$class[] = $num = new phoneNumbervalidator('Home Phone Number','51p27852155');
$class[] = $len = new textLengthvalidator('Summary','adflas]kjflasjflskajflsadjflsdkjklsjflasjkdflasdjflsk aklsjfls adkjflsdjk f',100);
$class[] = $num2 = new numericvalidator('Numbers','1h23');
foreach ($class as $v)
{
if (!$v->validate()) {
echo $v->getErrors().'
';
}
}
?>
Semplice ma illuminante !
Come si dice mi si è aperto
un nuovo mondo 