dblayer : Add verification of format in insert selected by field type
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1621 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
49
dblayer.php
49
dblayer.php
@@ -206,14 +206,59 @@ class dblayer extends PDO
|
||||
if (in_array ("not null", $params) && !array_key_exists ($key, $datas))
|
||||
throw new Exception (sprintf (_("Mandatory field '%s' not provided"),
|
||||
$key), 405);
|
||||
if (in_array ("not null", $params) && $datas[$key] === "")
|
||||
throw new Exception (sprintf (_("Mandatory field '%s' is empty"),
|
||||
$key), 405);
|
||||
if (!array_key_exists ($key, $datas))
|
||||
continue;
|
||||
// Verify the fields, if $verify is defined, before doing insertion
|
||||
$verify = $this->verifyOne ($key, $datas[$key]);
|
||||
if (is_array ($verify) && count ($verify))
|
||||
throw new Exception ($verify[0]." ".$verify[1]." in ".$key);
|
||||
// TODO : Check for type inconsistencies before create $datasOK
|
||||
if (array_key_exists ($key, $datas))
|
||||
// Check for type inconsistencies if the value is provided
|
||||
if ($datas[$key] !== "" && $params[0] === "integer")
|
||||
{
|
||||
if (strspn ($datas[$key], "0123456789") !== strlen ($datas[$key]))
|
||||
throw new Exception (sprintf (
|
||||
_("Errors in consistency : '%s' is not an integer"),
|
||||
$key), 405);
|
||||
}
|
||||
elseif ($datas[$key] !== "" && $params[0] === "varchar")
|
||||
{
|
||||
if (! isset ($params[1]))
|
||||
throw new Exception (sprintf (
|
||||
_("The length of varchar field '%s' is not provided"),
|
||||
$key), 500);
|
||||
if (strlen ($datas[$key]) > $params[1])
|
||||
throw new Exception (sprintf (
|
||||
_("Errors in consistency : '%s' data is too long"),
|
||||
$key), 405);
|
||||
}
|
||||
elseif ($datas[$key] !== "" && $params[0] === "datetime")
|
||||
{
|
||||
// The date format must be in ANSI SQL : YYYY-MM-DD HH:MM:SS
|
||||
$d = DateTime::createFromFormat("Y-m-d H:i:s", $datas[$key]);
|
||||
if (!$d || $d->format("Y-m-d H:i:s") !== $datas[$key])
|
||||
throw new Exception (sprintf (
|
||||
_("Incorrect datetime provided for field '%s'"),
|
||||
$key), 500);
|
||||
}
|
||||
elseif ($datas[$key] !== "" && $params[0] === "date")
|
||||
{
|
||||
// The date format must be in ANSI SQL : YYYY-MM-DD
|
||||
$d = DateTime::createFromFormat("Y-m-d", $datas[$key]);
|
||||
if (!$d || $d->format("Y-m-d") !== $datas[$key])
|
||||
throw new Exception (sprintf (
|
||||
_("Incorrect date provided for field '%s'"),
|
||||
$key), 500);
|
||||
}
|
||||
elseif ($datas[$key] !== "")
|
||||
throw new Exception (sprintf (_("Unknown field type for '%s'"), $key),
|
||||
500);
|
||||
else
|
||||
{
|
||||
// Nothing to do if the value is empty : just save it
|
||||
}
|
||||
$datasOK[$key] = $datas[$key];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user