Re: Validation class
| From: | Andrey Djachenko | Date: | Thu, 11 Apr 2002 04:38:10 +0000 |
| Subject: | Re: Validation class | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-5328@lists.php.net to get a copy of this message | ||
On Thursday 11 April 2002 05:07, you wrote:
> what do u think of having a validation class inside PEAR,
> which validates various stuff,
> such as:
> - check if value is a number
> - check if value exists at all
> - check if value is at least X characteers long
> - check if value is a proper date
> - check if value is a proper email address
> etc.
>
> i would need this currently for form checking.
> i know there is OOH-Forms, but imagine to use it for any kind of
> stuff, not just form validation ...
>
> would that be cool for PEAR?
> i'd commit it ... if approved
Now I am using such class (it is very raw of couse)
The main idea is validate value according some virtual "type" of one.
Also it can normalize (escape some symbols and format) such values.
class Validator
{
///////////////////////////////////////////////////////////////////////
function validate($val, $type, $allowempty = true)
{
switch ($type)
{
case 'text':
return Validator::checkText($val, $allowempty);
case 'money':
return Validator::checkMoney($val, $allowempty);
case 'email':
return Validator::checkEmail($val, $allowempty);
case 'date':
return Validator::checkDate($val, $allowempty);
case 'int':
return Validator::checkInteger($val, $allowempty);
case 'float':
return Validator::checkFloat($val, $allowempty);
default:
return false;
}
}
///////////////////////////////////////////////////////////////////////
function normalize($val, $type)
{
switch ($type)
{
case 'text':
return Validator::normalizeText($val);
case 'money':
return Validator::normalizeMoney($val);
default:
return $val;
}
}
///////////////////////////////////////////////////////////////////////
function normalizeText($val)
{
$result = trim($val);
$result = htmlspecialchars ($result, ENT_QUOTES);
return $result;
}
///////////////////////////////////////////////////////////////////////
function checkText($val, $allowempty = true)
{
$result = true;
$val = trim($val);
if (!$allowempty && empty($val))
{
$result = false;
}
return $result;
}
///////////////////////////////////////////////////////////////////////
function checkDate($val, $allowempty = true)
{
//checkdate (int month, int day, int year);
return true;
}
///////////////////////////////////////////////////////////////////////
function checkEmail($val, $allowempty = true)
{
//some regexp
return true;
}
///////////////////////////////////////////////////////////////////////
function checkMoney($val, $allowempty = true)
{
$val = trim($val);
if (!$allowempty && $val == '')
{
return false;
}
if ($val !='' && !is_numeric($val))
{
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////
function normalizeMoney($val)
{
$val = trim($val);
return number_format($val, 2, '.', '');
}
///////////////////////////////////////////////////////////////////////
function checkInteger($val, $allowempty = true)
{
$val = trim($val);
if (!$allowempty && $val == '')
{
return false;
}
if ($val != '' && !is_integer($val))
{
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////
function checkFloat($val, $allowempty = true)
{
if (!$allowempty && $val == '')
{
return false;
}
if ($val != '' && !is_float($val))
{
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////
}
?>
With help of this class I validate for example data objects described in
those maner:
class Goods extends Object
{
////////////////////////////////////////////////////////////////////
var $_fields = array(
//name type required value
'id' =>
array('type'=>'text', 'required'=>true, 'value'=>''),
'categoryid' =>
array('type'=>'text', 'required'=>true, 'value'=>'0'),
'name' =>
array('type'=>'text', 'required'=>true, 'value'=>''),
'price' =>
array('type'=>'money', 'required'=>false, 'value'=>'0.00'),
'description' =>
array('type'=>'text', 'required'=>false, 'value'=>''),
'image' =>
array('type'=>'text', 'required'=>false, 'value'=>'')
);
class Object
{
...
/////////////////////////////////////////////////////////////////////
function &isValid()
{
$wrongfields = array();
foreach ($this->_fields as $f_name=>$f_atribs)
{
$ae = !$f_atribs['required'];
if (!Validator::Validate($f_atribs['value'], $f_atribs['type'], $ae))
{
$wrongfields[$f_name] = true;
}
}
if (sizeof($wrongfields) == 0) return true;
return $wrongfields;
}
/////////////////////////////////////////////////////////////////////
...
}
?>