@@ -47,7 +47,7 @@ We can then check if it is unique **in the current locale**:
47
47
48
48
``` php
49
49
$attributes = request()->validate([
50
- 'slug' => [' unique_translation:posts'] ,
50
+ 'slug' => 'required| unique_translation:posts',
51
51
]);
52
52
```
53
53
@@ -57,7 +57,7 @@ You could also use the Rule instance:
57
57
use CodeZero\UniqueTranslation\UniqueTranslationRule;
58
58
59
59
$attributes = request()->validate([
60
- 'slug' => [new UniqueTranslationRule('posts')],
60
+ 'slug' => ['required', UniqueTranslationRule::for ('posts')],
61
61
]);
62
62
```
63
63
@@ -74,9 +74,9 @@ We need to validate the entire array in this case. Mind the `slug.*` key.
74
74
75
75
``` php
76
76
$attributes = request()->validate([
77
- 'slug.*' => [ 'unique_translation:posts'] ,
77
+ 'slug.*' => 'unique_translation:posts',
78
78
// or...
79
- 'slug.*' => [new UniqueTranslationRule('posts')] ,
79
+ 'slug.*' => UniqueTranslationRule::for ('posts'),
80
80
]);
81
81
```
82
82
@@ -86,9 +86,9 @@ Maybe your form field has a name of `post_slug` and your database field `slug`:
86
86
87
87
``` php
88
88
$attributes = request()->validate([
89
- 'post_slug.*' => [ 'unique_translation:posts,slug'] ,
89
+ 'post_slug.*' => 'unique_translation:posts,slug',
90
90
// or...
91
- 'post_slug.*' => [new UniqueTranslationRule('posts', 'slug')] ,
91
+ 'post_slug.*' => UniqueTranslationRule::for ('posts', 'slug'),
92
92
]);
93
93
```
94
94
@@ -98,9 +98,9 @@ If you're updating a record, you may want to ignore the post itself from the uni
98
98
99
99
``` php
100
100
$attributes = request()->validate([
101
- 'slug.*' => [ "unique_translation:posts,slug,{$post->id}"] ,
101
+ 'slug.*' => "unique_translation:posts,slug,{$post->id}",
102
102
// or...
103
- 'slug.*' => [(new UniqueTranslationRule('posts')) ->ignore($post->id)] ,
103
+ 'slug.*' => UniqueTranslationRule::for ('posts')->ignore($post->id),
104
104
]);
105
105
```
106
106
@@ -110,9 +110,9 @@ If your ID column has a different name, or you just want to use another column:
110
110
111
111
``` php
112
112
$attributes = request()->validate([
113
- 'slug.*' => [ 'unique_translation:posts,slug,ignore_value,ignore_column'] ,
113
+ 'slug.*' => 'unique_translation:posts,slug,ignore_value,ignore_column',
114
114
// or...
115
- 'slug.*' => [(new UniqueTranslationRule('posts')) ->ignore('ignore_value', 'ignore_column')] ,
115
+ 'slug.*' => UniqueTranslationRule::for ('posts')->ignore('ignore_value', 'ignore_column'),
116
116
]);
117
117
```
118
118
@@ -139,14 +139,34 @@ Your validation logic:
139
139
140
140
``` php
141
141
$attributes = request()->validate([
142
- 'slug.*' => [ 'unique_translation:posts'] ,
142
+ 'slug.*' => 'unique_translation:posts',
143
143
]);
144
144
```
145
145
146
146
The result is that ` slug[en] ` is valid, since the only ` en ` value in the database is ` not-abc ` .
147
147
148
148
And ` slug[nl] ` would fail, because there already is a ` nl ` value of ` abc ` .
149
149
150
+ ## Error Messages
151
+
152
+ Whether you are validating a single translation (` 'slug' ` ) or an array of translations (` 'slug.*' ` ), if validation fails, you will find an error for both the single and the localized key:
153
+
154
+ ``` php
155
+ $errors->first('slug');
156
+ $errors->first('slug.en');
157
+ ```
158
+
159
+ You can pass your own error message with any of the following keys. The first one found will be used.
160
+
161
+ ``` php
162
+ $attributes = request()->validate([
163
+ 'slug.*' => 'unique_translation:posts',
164
+ ], [
165
+ 'slug.unique_translation' => 'Your custom :attribute error.',
166
+ 'slug.*.unique_translation' => 'Your custom :attribute error.',
167
+ 'slug.en.unique_translation' => 'Your custom :attribute error.',
168
+ ]);
169
+ ```
150
170
151
171
## Testing
152
172
0 commit comments