Skip to content

Commit 583d909

Browse files
committed
Add validation rule "exist"
1 parent 702a01a commit 583d909

File tree

6 files changed

+99
-0
lines changed

6 files changed

+99
-0
lines changed

.phpstorm.meta.php

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
);
3333
registerArgumentsSet(
3434
'rules',
35+
'exist',
3536
'notUnique',
3637
'unique',
3738
);

src/Languages/en/validation.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* file that was distributed with this source code.
99
*/
1010
return [
11+
'exist' => 'The {field} field does not exist.',
1112
'notUnique' => 'The {field} field is not registered.',
1213
'unique' => 'The {field} field has already been registered.',
1314
];

src/Languages/es/validation.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* file that was distributed with this source code.
99
*/
1010
return [
11+
'exist' => 'El campo {field} no existe.',
1112
'notUnique' => 'El campo {field} no está registrado.',
1213
'unique' => 'El campo {field} ya se ha registrado. ',
1314
];

src/Languages/pt-br/validation.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* file that was distributed with this source code.
99
*/
1010
return [
11+
'exist' => 'O campo {field} não existe.',
1112
'notUnique' => 'O campo {field} não está registrado.',
1213
'unique' => 'O campo {field} já foi registrado.',
1314
];

src/Validator.php

+41
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,45 @@ public static function unique(
9595
}
9696
return $statement->limit(1)->run()->fetch()->count < 1; // @phpstan-ignore-line
9797
}
98+
99+
/**
100+
* Validates value exists in database table.
101+
*
102+
* @since 3.3
103+
*
104+
* @param string $field
105+
* @param array<string,mixed> $data
106+
* @param string $tableColumn
107+
* @param string $connection
108+
*
109+
* @return bool
110+
*/
111+
public static function exist(
112+
string $field,
113+
array $data,
114+
string $tableColumn,
115+
string $connection = 'default'
116+
) : bool {
117+
$value = static::getData($field, $data);
118+
if ($value === null) {
119+
return false;
120+
}
121+
[$table, $column] = \array_pad(\explode('.', $tableColumn, 2), 2, '');
122+
if ($column === '') {
123+
$column = $field;
124+
}
125+
if ($connection === '') {
126+
throw new LogicException(
127+
'The connection parameter must be set to be able to connect the database'
128+
);
129+
}
130+
return App::database($connection) // @phpstan-ignore-line
131+
->select()
132+
->expressions(['count' => static fn () => 'COUNT(*)'])
133+
->from($table)
134+
->whereEqual($column, $value)
135+
->limit(1)
136+
->run()
137+
->fetch()->count > 0;
138+
}
98139
}

tests/ValidatorTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ protected function setUp() : void
3333
App::database()->insert()->into('Users')->values(2, 'bar')->run();
3434
}
3535

36+
public function testExistNoDataValue() : void
37+
{
38+
$validation = App::validation();
39+
$validation->setRule('username', 'exist:Users');
40+
self::assertFalse($validation->validate([]));
41+
self::assertArrayHasKey('username', $validation->getErrors());
42+
}
43+
3644
public function testUniqueNoDataValue() : void
3745
{
3846
$validation = App::validation();
@@ -49,6 +57,22 @@ public function testNotUniqueNoDataValue() : void
4957
self::assertArrayNotHasKey('username', $validation->getErrors());
5058
}
5159

60+
public function testExist() : void
61+
{
62+
$validation = App::validation();
63+
$validation->setRule('username', 'exist:Users');
64+
$data = [
65+
'username' => 'foo',
66+
];
67+
self::assertTrue($validation->validate($data));
68+
self::assertEmpty($validation->getErrors());
69+
$data = [
70+
'username' => 'bazz',
71+
];
72+
self::assertFalse($validation->validate($data));
73+
self::assertArrayHasKey('username', $validation->getErrors());
74+
}
75+
5276
public function testUnique() : void
5377
{
5478
$validation = App::validation();
@@ -71,6 +95,22 @@ public function testNotUnique() : void
7195
self::assertArrayNotHasKey('username', $validation->getErrors());
7296
}
7397

98+
public function testExistWithTableColumn() : void
99+
{
100+
$validation = App::validation();
101+
$validation->setRule('user', 'exist:Users.username');
102+
$data = [
103+
'user' => 'foo',
104+
];
105+
self::assertTrue($validation->validate($data));
106+
self::assertEmpty($validation->getErrors());
107+
$data = [
108+
'user' => 'bazz',
109+
];
110+
self::assertFalse($validation->validate($data));
111+
self::assertArrayHasKey('user', $validation->getErrors());
112+
}
113+
74114
public function testUniqueWithTableColumn() : void
75115
{
76116
$validation = App::validation();
@@ -114,6 +154,20 @@ public function testNotUniqueIgnoring() : void
114154
self::assertArrayHasKey('username', $validation->getErrors());
115155
}
116156

157+
public function testExistWithoutConnection() : void
158+
{
159+
$validation = App::validation();
160+
$validation->setRule('username', 'exist:Users,');
161+
$this->expectException(\LogicException::class);
162+
$this->expectExceptionMessage(
163+
'The connection parameter must be set to be able to connect the database'
164+
);
165+
$data = [
166+
'username' => 'foo',
167+
];
168+
$validation->validate($data);
169+
}
170+
117171
public function testUniqueWithoutConnection() : void
118172
{
119173
$validation = App::validation();

0 commit comments

Comments
 (0)