Skip to content

Commit d4c7e01

Browse files
committed
Create validation rule UNIQUE
1 parent bd25887 commit d4c7e01

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

core/Database.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ protected function saveMigrations(array $newMigrations)
8383
$statement->execute();
8484
}
8585

86+
public function prepare($sql): \PDOStatement
87+
{
88+
return $this->pdo->prepare($sql);
89+
}
90+
8691
private function log($message)
8792
{
8893
echo "[" . date("Y-m-d H:i:s") . "] - " . $message . PHP_EOL;

core/DbModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function save()
3030
$tableName = $this->tableName();
3131
$attributes = $this->attributes();
3232
$params = array_map(fn($attr) => ":$attr", $attributes);
33-
$statement = $this->prepare("INSERT INTO $tableName (" . implode(",", $attributes) . ")
33+
$statement = self::prepare("INSERT INTO $tableName (" . implode(",", $attributes) . ")
3434
VALUES (" . implode(",", $params) . ")");
3535
foreach ($attributes as $attribute) {
3636
$statement->bindValue(":$attribute", $this->{$attribute});
@@ -39,8 +39,8 @@ public function save()
3939
return true;
4040
}
4141

42-
public function prepare($sql): \PDOStatement
42+
public static function prepare($sql): \PDOStatement
4343
{
44-
return Application::$app->db->pdo->prepare($sql);
44+
return Application::$app->db->prepare($sql);
4545
}
4646
}

core/Model.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Model
2121
const RULE_MIN = 'min';
2222
const RULE_MAX = 'max';
2323
const RULE_MATCH = 'match';
24+
const RULE_UNIQUE = 'unique';
2425

2526
public array $errors = [];
2627

@@ -62,6 +63,18 @@ public function validate()
6263
if ($ruleName === self::RULE_MATCH && $value !== $this->{$rule['match']}) {
6364
$this->addError($attribute, self::RULE_MATCH, ['match' => $rule['match']]);
6465
}
66+
if ($ruleName === self::RULE_UNIQUE) {
67+
$className = $rule['class'];
68+
$tableName = $className::tableName();
69+
$db = Application::$app->db;
70+
$statement = $db->prepare("SELECT * FROM $tableName WHERE $attribute = :$attribute");
71+
$statement->bindValue(":$attribute", $value);
72+
$statement->execute();
73+
$record = $statement->fetchObject();
74+
if ($record) {
75+
$this->addError($attribute, self::RULE_UNIQUE);
76+
}
77+
}
6578
}
6679
}
6780
return empty($this->errors);
@@ -75,6 +88,7 @@ public function errorMessages()
7588
self::RULE_MIN => 'Min length of this field must be {min}',
7689
self::RULE_MAX => 'Max length of this field must be {max}',
7790
self::RULE_MATCH => 'This field must be the same as {match}',
91+
self::RULE_UNIQUE => 'Record with with this {field} already exists',
7892
];
7993
}
8094

@@ -85,6 +99,7 @@ public function errorMessage($rule)
8599

86100
public function addError(string $attribute, string $rule, $params = [])
87101
{
102+
$params['field'] ??= $attribute;
88103
$errorMessage = $this->errorMessage($rule);
89104
foreach ($params as $key => $value) {
90105
$errorMessage = str_replace("{{$key}}", $value, $errorMessage);

models/User.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public function rules()
4040
return [
4141
'firstname' => [self::RULE_REQUIRED],
4242
'lastname' => [self::RULE_REQUIRED],
43-
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
43+
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL, [
44+
self::RULE_UNIQUE, 'class' => self::class
45+
]],
4446
'password' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 8]],
4547
'passwordConfirm' => [[self::RULE_MATCH, 'match' => 'password']],
4648
];

0 commit comments

Comments
 (0)