# Custom Validation Rules

You can use custom rules to validate unique and exists rules for translatable attributes.

## TranslatableUnique

Ensure that the attribute value is unique by checking its absence in the database; if the value already exists, raise a validation exception.

### Option 1

```php
use Astrotomic\Translatable\Validation\Rules\TranslatableUnique;
...

$person = new Person(['name' => 'john doe']);
$person->save();

$data = [
    'name' => 'john doe',
    'email' => 'john@example.com'
];
$validator = Validator::make($data, [
    'name' => ['required', new TranslatableUnique(Person::class, 'name')],
]);

```

### Option 2

```php
use Astrotomic\Translatable\Validation\Rules\TranslatableUnique;
...

$person = new Person(['name' => 'john doe']);
$person->save();

$data = [
    'name:en' => 'john doe',
    'email' => 'john@example.com'
];

$validator = Validator::make($data, [
    'name:en' => ['required', Rule::translatableUnique(Person::class, 'name:en')],
]);

```

### Option 2

```php
use Illuminate\Validation\Rule;
...

$person = new Person(['name' => 'john doe']);
$person->save();

$data = [
    'name:en' => 'john doe',
    'email' => 'john@example.com'
];

$validator = Validator::make($data, [
    'name:en' => ['required', Rule::translatableUnique(Person::class, 'name:en')],
]);

```

## TranslatableExists

Verify if the attribute value exists by confirming its presence in the database; if the value does not exist, raise a validation exception.

### Option 1

```php
use Astrotomic\Translatable\Validation\Rules\TranslatableExists;
...

$person = new Person(['name' => 'john doe']);
$person->save();

$data = [
    'name' => 'john doe',
    'email' => 'john@example.com'
];
$validator = Validator::make($data, [
    'name' => ['required', new TranslatableExists(Person::class, 'name')],
]);
```

### Option 2

```php
use Illuminate\Validation\Rule;
...

$person = new Person(['name' => 'john doe']);
$person->save();

$data = [
    'name:en' => 'john doe',
    'email' => 'john@example.com'
];

$validator = Validator::make($data, [
    'name:en' => ['required', Rule::translatableExists(Person::class, 'name:en')],
]);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.astrotomic.info/laravel-translatable/usage/custom-validation-rule.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
