Skip to content

Commit

Permalink
added field type check against numeric or string
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed May 5, 2020
1 parent 25daa42 commit e14f3a9
Showing 1 changed file with 70 additions and 2 deletions.
72 changes: 70 additions & 2 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,41 @@ public function is_not_unique(string $str = null, string $field, array $data): b
{
if (! preg_match('/\{(\w+)\}/', $where_value))
{
$row = $row->where($where_field, $where_value);
$result = $row->get(null, 0, false);
if ($result->getRow() !== null)
{
return true;
}

$fieldType = 'string';
if (in_array(
$result->getFieldData()[0]->type,
[
'bit',
'int2',
'int4',
'int8',
'float4',
'float8',
'numeric',
\MYSQLI_TYPE_DECIMAL,
\MYSQLI_TYPE_NEWDECIMAL,
\MYSQLI_TYPE_FLOAT,
\MYSQLI_TYPE_DOUBLE,
\MYSQLI_TYPE_TINY,
\MYSQLI_TYPE_SHORT,
\MYSQLI_TYPE_LONG,
\MYSQLI_TYPE_LONGLONG,
\MYSQLI_TYPE_INT24,
]))
{
$fieldType = 'numeric';
}

if ($fieldType === 'string' || $fieldType === 'numeric' && is_numeric($where_value))
{
$row = $row->where($where_field, $where_value);
}
}
}

Expand Down Expand Up @@ -233,7 +267,41 @@ public function is_unique(string $str = null, string $field, array $data): bool
{
if (! preg_match('/\{(\w+)\}/', $ignoreValue))
{
$row = $row->where("{$ignoreField} !=", $ignoreValue);
$result = $row->get(null, 0, false);
if (! $result->getRow())
{
return true;
}

$fieldType = 'string';
if (in_array(
$result->getFieldData()[0]->type,
[
'bit',
'int2',
'int4',
'int8',
'float4',
'float8',
'numeric',
\MYSQLI_TYPE_DECIMAL,
\MYSQLI_TYPE_NEWDECIMAL,
\MYSQLI_TYPE_FLOAT,
\MYSQLI_TYPE_DOUBLE,
\MYSQLI_TYPE_TINY,
\MYSQLI_TYPE_SHORT,
\MYSQLI_TYPE_LONG,
\MYSQLI_TYPE_LONGLONG,
\MYSQLI_TYPE_INT24,
]))
{
$fieldType = 'numeric';
}

if ($fieldType === 'string' || $fieldType === 'numeric' && is_numeric($ignoreValue))
{
$row = $row->where("{$ignoreField} !=", $ignoreValue);
}
}
}

Expand Down

0 comments on commit e14f3a9

Please sign in to comment.