Skip to content

Commit 4186a27

Browse files
committed
Added better language support; Refactored test cases; Added missing type definitions
1 parent 795b1c6 commit 4186a27

File tree

7 files changed

+502
-95
lines changed

7 files changed

+502
-95
lines changed

readme.md

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ You can install the package via composer with following command:
2222
composer require korridor/laravel-model-validation-rules
2323
```
2424

25-
### Translation
26-
27-
If you want to customize the translations of the validation errors you can publish the translations
28-
of the package to the `resources/lang/vendor/modelValidationRules` folder.
29-
30-
```bash
31-
php artisan vendor:publish --provider="Korridor\LaravelModelValidationRules\ModelValidationServiceProvider"
32-
```
33-
3425
### Requirements
3526

3627
This package is tested for the following Laravel versions:
@@ -48,7 +39,7 @@ This package is tested for the following Laravel versions:
4839
use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
4940
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
5041
// ...
51-
public function rules()
42+
public function rules(): array
5243
{
5344
$postId = $this->post->id;
5445

@@ -73,7 +64,7 @@ public function rules()
7364
use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
7465
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
7566
// ...
76-
public function rules()
67+
public function rules(): array
7768
{
7869
$postId = $this->post->id;
7970

@@ -93,13 +84,74 @@ public function rules()
9384
}
9485
```
9586

87+
### Custom validation message
88+
89+
If you want to change the validation message for one specific case, you can use the `withMessage(...)` function to add a custom validation message.
90+
With `withCustomTranslation(...)` you can set a custom translation key for the validation message.
91+
As described in detail in the next example ([Customize default validation message](#customize-default-validation-message)), it is possible to use `:attribute`, `:model` and `:value` in the translation.
92+
93+
```php
94+
use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
95+
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
96+
// ...
97+
public function rules(): array
98+
{
99+
$postId = $this->post->id;
100+
101+
return [
102+
'id' => [(new ExistsEloquent(Post::class))->withMessage('The ID already exists.')],
103+
'username' => [
104+
(new UniqueEloquent(User::class, 'username'))
105+
->ignore($postId)
106+
->withCustomTranslation('validation.custom.username.unique_eloquent')
107+
],
108+
'title' => ['string'],
109+
'content' => ['string'],
110+
'comments.*.id' => [
111+
'nullable',
112+
new ExistsEloquent(Comment::class, null, function (Builder $builder) use ($postId) {
113+
return $builder->where('post_id', $postId);
114+
}),
115+
],
116+
'comments.*.content' => ['string']
117+
];
118+
}
119+
```
120+
121+
### Customize default validation message
122+
123+
If you want to customize the translations of the default validation errors you can publish the translations
124+
of the package to the `resources/lang/vendor/modelValidationRules` folder.
125+
126+
```bash
127+
php artisan vendor:publish --provider="Korridor\LaravelModelValidationRules\ModelValidationServiceProvider"
128+
```
129+
130+
You can use the following attributes in the validation message:
131+
132+
- `attribute`
133+
- `model`
134+
- `value`
135+
136+
```php
137+
return [
138+
'exists_model' => 'A :model with the :attribute ":value" does not exist.',
139+
'unique_model' => 'A :model with the :attribute ":value" already exists.',
140+
];
141+
```
142+
143+
Example outputs would be:
144+
145+
- `A user with the id "2" does not exist.`
146+
- `A user with the id "2" already exists.`
147+
96148
## Contributing
97149

98150
I am open for suggestions and contributions. Just create an issue or a pull request.
99151

100152
### Local docker environment
101153

102-
The `docker` folder contains a local docker enironment for development.
154+
The `docker` folder contains a local docker environment for development.
103155
The docker workspace has composer and xdebug installed.
104156

105157
```bash

src/ModelValidationServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class ModelValidationServiceProvider extends ServiceProvider
1212
/**
1313
* Register services.
1414
*/
15-
public function register()
15+
public function register(): void
1616
{
1717
}
1818

1919
/**
2020
* Bootstrap services.
2121
*/
22-
public function boot()
22+
public function boot(): void
2323
{
2424
$this->publishes([
2525
__DIR__.'/../resources/lang' => resource_path('lang/vendor/modelValidationRules'),

src/Rules/ExistsEloquent.php

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,58 @@
1010
class ExistsEloquent implements Rule
1111
{
1212
/**
13+
* Class name of model.
14+
*
1315
* @var string
1416
*/
1517
private $model;
1618

1719
/**
20+
* Relevant key in the model.
21+
*
1822
* @var string|null
1923
*/
2024
private $key;
2125

2226
/**
27+
* Closure that can extend the eloquent builder.
28+
*
2329
* @var Closure|null
2430
*/
2531
private $builderClosure;
2632

2733
/**
34+
* Current attribute that is validated.
35+
*
2836
* @var string
2937
*/
30-
private $attribute;
38+
private $attribute = null;
3139

3240
/**
41+
* Current value that is validated.
42+
*
3343
* @var mixed
3444
*/
35-
private $value;
45+
private $value = null;
46+
47+
/**
48+
* Custom validation message.
49+
*
50+
* @var string|null
51+
*/
52+
private $message = null;
53+
54+
/**
55+
* @var bool|null
56+
*/
57+
private $messageTranslated = null;
3658

3759
/**
3860
* Create a new rule instance.
3961
*
40-
* @param string $model
41-
* @param string|null $key
42-
* @param Closure|null $builderClosure
62+
* @param string $model Class name of model
63+
* @param string|null $key Relevant key in the model
64+
* @param Closure|null $builderClosure Closure that can extend the eloquent builder
4365
*/
4466
public function __construct(string $model, ?string $key = null, ?Closure $builderClosure = null)
4567
{
@@ -48,6 +70,46 @@ public function __construct(string $model, ?string $key = null, ?Closure $builde
4870
$this->setBuilderClosure($builderClosure);
4971
}
5072

73+
/**
74+
* Set a custom validation message.
75+
*
76+
* @param string $message
77+
* @param bool $translated
78+
*/
79+
public function setMessage(string $message, bool $translated): void
80+
{
81+
$this->message = $message;
82+
$this->messageTranslated = $translated;
83+
}
84+
85+
/**
86+
* Set a custom validation message.
87+
*
88+
* @param string $message
89+
*
90+
* @return $this
91+
*/
92+
public function withMessage(string $message): self
93+
{
94+
$this->setMessage($message, false);
95+
96+
return $this;
97+
}
98+
99+
/**
100+
* Set a translated custom validation message.
101+
*
102+
* @param string $translationKey
103+
*
104+
* @return $this
105+
*/
106+
public function withCustomTranslation(string $translationKey): self
107+
{
108+
$this->setMessage($translationKey, true);
109+
110+
return $this;
111+
}
112+
51113
/**
52114
* Determine if the validation rule passes.
53115
*
@@ -82,11 +144,29 @@ public function passes($attribute, $value): bool
82144
*/
83145
public function message(): string
84146
{
85-
return trans('modelValidationRules::validation.exists_model', [
86-
'attribute' => $this->attribute,
87-
'model' => class_basename($this->model),
88-
'value' => $this->value,
89-
]);
147+
if ($this->message !== null) {
148+
if ($this->messageTranslated) {
149+
return trans(
150+
$this->message,
151+
[
152+
'attribute' => $this->attribute,
153+
'model' => strtolower(class_basename($this->model)),
154+
'value' => $this->value,
155+
]
156+
);
157+
} else {
158+
return $this->message;
159+
}
160+
} else {
161+
return trans(
162+
'modelValidationRules::validation.exists_model',
163+
[
164+
'attribute' => $this->attribute,
165+
'model' => strtolower(class_basename($this->model)),
166+
'value' => $this->value,
167+
]
168+
);
169+
}
90170
}
91171

92172
/**

src/Rules/UniqueEloquent.php

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ class UniqueEloquent implements Rule
4444
*/
4545
private $ignoreColumn;
4646

47+
/**
48+
* Custom validation message.
49+
*
50+
* @var string|null
51+
*/
52+
private $message = null;
53+
54+
/**
55+
* @var bool|null
56+
*/
57+
private $messageTranslated = null;
58+
4759
/**
4860
* UniqueEloquent constructor.
4961
* @param string $model
@@ -87,18 +99,76 @@ public function passes($attribute, $value): bool
8799
return 0 === $builder->count();
88100
}
89101

102+
/**
103+
* Set a custom validation message.
104+
*
105+
* @param string $message
106+
* @param bool $translated
107+
*/
108+
public function setMessage(string $message, bool $translated): void
109+
{
110+
$this->message = $message;
111+
$this->messageTranslated = $translated;
112+
}
113+
114+
/**
115+
* Set a custom validation message.
116+
*
117+
* @param string $message
118+
*
119+
* @return $this
120+
*/
121+
public function withMessage(string $message): self
122+
{
123+
$this->setMessage($message, false);
124+
125+
return $this;
126+
}
127+
128+
/**
129+
* Set a translated custom validation message.
130+
*
131+
* @param string $translationKey
132+
*
133+
* @return $this
134+
*/
135+
public function withCustomTranslation(string $translationKey): self
136+
{
137+
$this->setMessage($translationKey, true);
138+
139+
return $this;
140+
}
141+
90142
/**
91143
* Get the validation error message.
92144
*
93145
* @return string|array
94146
*/
95147
public function message(): string
96148
{
97-
return trans('modelValidationRules::validation.unique_model', [
98-
'attribute' => $this->attribute,
99-
'model' => class_basename($this->model),
100-
'value' => $this->value,
101-
]);
149+
if ($this->message !== null) {
150+
if ($this->messageTranslated) {
151+
return trans(
152+
$this->message,
153+
[
154+
'attribute' => $this->attribute,
155+
'model' => strtolower(class_basename($this->model)),
156+
'value' => $this->value,
157+
]
158+
);
159+
} else {
160+
return $this->message;
161+
}
162+
} else {
163+
return trans(
164+
'modelValidationRules::validation.unique_model',
165+
[
166+
'attribute' => $this->attribute,
167+
'model' => strtolower(class_basename($this->model)),
168+
'value' => $this->value,
169+
]
170+
);
171+
}
102172
}
103173

104174
/**

0 commit comments

Comments
 (0)