Skip to content
This repository was archived by the owner on Sep 13, 2024. It is now read-only.

Commit bdea4d9

Browse files
authored
Merge pull request #5 from VladimirBerdnik/master
Resolve "Presence verifier has not been set." issue for **exists** and **in** rules
2 parents d1eca31 + 0f15ab7 commit bdea4d9

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

CHANGES.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# Changes History
22

3+
1.0.6
4+
-----
5+
Resolve "Presence verifier has not been set." issue for **exists** and **in** rules
6+
37
1.0.5
48
-----
5-
Add ability to use custom declared validation rules
9+
Add ability to use custom declared validation rules
610

711
1.0.4
812
-----
9-
Add 'sometimes' rule modifier
13+
Add 'sometimes' rule modifier
1014

1115
1.0.3
1216
-----

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ Add the FluentValidationServiceProvider in ``config/app.php``:
5757
*Note:* You can omit service provider registration, but then you must call
5858
*->toString()* or *->toArray()* on each builder.
5959
If service provider is registered, manual casting of rule to string or array
60-
is not necessary.
60+
is not necessary and default Laravel's *Illuminate\Validation\ValidationServiceProvider::class*
61+
can be removed from *'providers'* array.
6162

6263

6364
## Available classes

src/FluentValidationServiceProvider.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@
22

33
namespace Saritasa\Laravel\Validation;
44

5-
use Illuminate\Support\ServiceProvider;
5+
use Illuminate\Validation\ValidationServiceProvider;
66
use Illuminate\Contracts\Validation\Factory as IValidatorFactory;
77

88
/**
99
* Service provider substitutes default Laravel's validation factory
1010
* @see FluentValidatorFactory
1111
*/
12-
class FluentValidationServiceProvider extends ServiceProvider
12+
class FluentValidationServiceProvider extends ValidationServiceProvider
1313
{
14-
public function boot()
14+
protected function registerValidationFactory()
1515
{
16-
$this->app->bind(IValidatorFactory::class, FluentValidatorFactory::class);
16+
$this->app->singleton('validator', function ($app) {
17+
$validator = new FluentValidatorFactory($app['translator'], $app);
18+
19+
// The validation presence verifier is responsible for determining the existence of
20+
// values in a given data collection which is typically a relational database or
21+
// other persistent data stores. It is used to check for "uniqueness" as well.
22+
if (isset($app['db'], $app['validation.presence'])) {
23+
$validator->setPresenceVerifier($app['validation.presence']);
24+
}
25+
26+
return $validator;
27+
});
1728
}
1829
}

src/RuleSet.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ protected function appendIfNotExists($rule)
9090
{
9191
if (in_array($rule, $this->rules)) {
9292
return $this;
93-
}
94-
else {
93+
} else {
9594
return new static(array_merge($this->rules, [$rule]));
9695
}
9796
}
@@ -108,16 +107,15 @@ protected static function mergeIfNotExists(string $rule, array $rules = []): arr
108107
{
109108
if (in_array($rule, $rules)) {
110109
return $rules;
111-
}
112-
else {
110+
} else {
113111
return array_merge($rules, [$rule]);
114112
}
115113
}
116114

117115
/** Return current rule set as array */
118116
public function toArray(): array
119117
{
120-
return array_map(function($rule) {
118+
return array_map(function ($rule) {
121119
if ($rule instanceof IRule) {
122120
return (string)$rule;
123121
} else {

tests/RuleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ public function testSometimes()
6464
*/
6565
public function testIn()
6666
{
67-
$this->assertEquals('in:male,female', Rule::in('male', 'female'));
68-
$this->assertEquals('in:Male,Female', Rule::in(Gender::getConstants()));
67+
$this->assertEquals('in:"male","female"', Rule::in('male', 'female'));
68+
$this->assertEquals('in:"Male","Female"', Rule::in(Gender::getConstants()));
6969
}
7070

7171
/**
7272
* Rule 'notIn' can accept values as list of parameters or as array
7373
*/
7474
public function testNotIn()
7575
{
76-
$this->assertEquals('not_in:forbidden,deleted', Rule::notIn('forbidden', 'deleted'));
77-
$this->assertEquals('not_in:forbidden,deleted', Rule::notIn(['forbidden', 'deleted']));
76+
$this->assertEquals('not_in:"forbidden","deleted"', Rule::notIn('forbidden', 'deleted'));
77+
$this->assertEquals('not_in:"forbidden","deleted"', Rule::notIn(['forbidden', 'deleted']));
7878
}
7979

8080
/**

0 commit comments

Comments
 (0)