|
| 1 | +### **Validating Unique and Exists Rule** |
| 2 | + |
| 3 | +You can use custom rules to validate unique and exists rules for translatable attributes. |
| 4 | + |
| 5 | +#### TranslatableUnique |
| 6 | + |
| 7 | +Ensure that the attribute value is unique by checking its absence in the database; if the value already exists, raise a validation exception. |
| 8 | + |
| 9 | +##### Option 1 |
| 10 | + |
| 11 | +```php |
| 12 | +use Astrotomic\Translatable\Validation\Rules\TranslatableUnique; |
| 13 | +... |
| 14 | + |
| 15 | +$person = new Person(['name' => 'john doe']); |
| 16 | +$person->save(); |
| 17 | + |
| 18 | +$data = [ |
| 19 | + 'name' => 'john doe', |
| 20 | + |
| 21 | +]; |
| 22 | +$validator = Validator::make($data, [ |
| 23 | + 'name' => ['required', new TranslatableUnique(Person::class, 'name')], |
| 24 | +]); |
| 25 | + |
| 26 | +``` |
| 27 | + |
| 28 | +##### Option 2 |
| 29 | + |
| 30 | +```php |
| 31 | +use Astrotomic\Translatable\Validation\Rules\TranslatableUnique; |
| 32 | +... |
| 33 | + |
| 34 | +$person = new Person(['name' => 'john doe']); |
| 35 | +$person->save(); |
| 36 | + |
| 37 | +$data = [ |
| 38 | + 'name:en' => 'john doe', |
| 39 | + |
| 40 | +]; |
| 41 | + |
| 42 | +$validator = Validator::make($data, [ |
| 43 | + 'name:en' => ['required', Rule::translatableUnique(Person::class, 'name:en')], |
| 44 | +]); |
| 45 | + |
| 46 | +``` |
| 47 | + |
| 48 | +##### Option 2 |
| 49 | + |
| 50 | +```php |
| 51 | +use Illuminate\Validation\Rule; |
| 52 | +... |
| 53 | + |
| 54 | +$person = new Person(['name' => 'john doe']); |
| 55 | +$person->save(); |
| 56 | + |
| 57 | +$data = [ |
| 58 | + 'name:en' => 'john doe', |
| 59 | + |
| 60 | +]; |
| 61 | + |
| 62 | +$validator = Validator::make($data, [ |
| 63 | + 'name:en' => ['required', Rule::translatableUnique(Person::class, 'name:en')], |
| 64 | +]); |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | +#### TranslatableExists |
| 70 | + |
| 71 | +Verify if the attribute value exists by confirming its presence in the database; if the value does not exist, raise a validation exception. |
| 72 | + |
| 73 | + |
| 74 | +##### Option 1 |
| 75 | +```php |
| 76 | +use Astrotomic\Translatable\Validation\Rules\TranslatableExists; |
| 77 | +... |
| 78 | + |
| 79 | +$person = new Person(['name' => 'john doe']); |
| 80 | +$person->save(); |
| 81 | + |
| 82 | +$data = [ |
| 83 | + 'name' => 'john doe', |
| 84 | + |
| 85 | +]; |
| 86 | +$validator = Validator::make($data, [ |
| 87 | + 'name' => ['required', new TranslatableExists(Person::class, 'name')], |
| 88 | +]); |
| 89 | +``` |
| 90 | + |
| 91 | +##### Option 2 |
| 92 | +```php |
| 93 | +use Illuminate\Validation\Rule; |
| 94 | +... |
| 95 | + |
| 96 | +$person = new Person(['name' => 'john doe']); |
| 97 | +$person->save(); |
| 98 | + |
| 99 | +$data = [ |
| 100 | + 'name:en' => 'john doe', |
| 101 | + |
| 102 | +]; |
| 103 | + |
| 104 | +$validator = Validator::make($data, [ |
| 105 | + 'name:en' => ['required', Rule::translatableExists(Person::class, 'name:en')], |
| 106 | +]); |
| 107 | +``` |
0 commit comments