Skip to content

Commit b01675d

Browse files
committed
Update readme
1 parent 10c91eb commit b01675d

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

README.md

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ We can then check if it is unique **in the current locale**:
4747

4848
```php
4949
$attributes = request()->validate([
50-
'slug' => ['unique_translation:posts'],
50+
'slug' => 'required|unique_translation:posts',
5151
]);
5252
```
5353

@@ -57,7 +57,7 @@ You could also use the Rule instance:
5757
use CodeZero\UniqueTranslation\UniqueTranslationRule;
5858

5959
$attributes = request()->validate([
60-
'slug' => [new UniqueTranslationRule('posts')],
60+
'slug' => ['required', UniqueTranslationRule::for('posts')],
6161
]);
6262
```
6363

@@ -74,9 +74,9 @@ We need to validate the entire array in this case. Mind the `slug.*` key.
7474

7575
```php
7676
$attributes = request()->validate([
77-
'slug.*' => ['unique_translation:posts'],
77+
'slug.*' => 'unique_translation:posts',
7878
// or...
79-
'slug.*' => [new UniqueTranslationRule('posts')],
79+
'slug.*' => UniqueTranslationRule::for('posts'),
8080
]);
8181
```
8282

@@ -86,9 +86,9 @@ Maybe your form field has a name of `post_slug` and your database field `slug`:
8686

8787
```php
8888
$attributes = request()->validate([
89-
'post_slug.*' => ['unique_translation:posts,slug'],
89+
'post_slug.*' => 'unique_translation:posts,slug',
9090
// or...
91-
'post_slug.*' => [new UniqueTranslationRule('posts', 'slug')],
91+
'post_slug.*' => UniqueTranslationRule::for('posts', 'slug'),
9292
]);
9393
```
9494

@@ -98,9 +98,9 @@ If you're updating a record, you may want to ignore the post itself from the uni
9898

9999
```php
100100
$attributes = request()->validate([
101-
'slug.*' => ["unique_translation:posts,slug,{$post->id}"],
101+
'slug.*' => "unique_translation:posts,slug,{$post->id}",
102102
// or...
103-
'slug.*' => [(new UniqueTranslationRule('posts'))->ignore($post->id)],
103+
'slug.*' => UniqueTranslationRule::for('posts')->ignore($post->id),
104104
]);
105105
```
106106

@@ -110,9 +110,9 @@ If your ID column has a different name, or you just want to use another column:
110110

111111
```php
112112
$attributes = request()->validate([
113-
'slug.*' => ['unique_translation:posts,slug,ignore_value,ignore_column'],
113+
'slug.*' => 'unique_translation:posts,slug,ignore_value,ignore_column',
114114
// or...
115-
'slug.*' => [(new UniqueTranslationRule('posts'))->ignore('ignore_value', 'ignore_column')],
115+
'slug.*' => UniqueTranslationRule::for('posts')->ignore('ignore_value', 'ignore_column'),
116116
]);
117117
```
118118

@@ -139,14 +139,34 @@ Your validation logic:
139139

140140
```php
141141
$attributes = request()->validate([
142-
'slug.*' => ['unique_translation:posts'],
142+
'slug.*' => 'unique_translation:posts',
143143
]);
144144
```
145145

146146
The result is that `slug[en]` is valid, since the only `en` value in the database is `not-abc`.
147147

148148
And `slug[nl]` would fail, because there already is a `nl` value of `abc`.
149149

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+
```
150170

151171
## Testing
152172

0 commit comments

Comments
 (0)