laravel-translatable
  • Introduction
  • Issues
  • Changelog
  • FAQ
  • Installation
  • Package
    • Interface
    • Methods
    • Scopes
    • Fallback locale
    • Locales helper
    • Validation Rule Factory
  • Usage
    • Attributes
    • Forms
    • Pivot Model
    • Custom Validation Rules
Powered by GitBook
On this page
  • TranslatableUnique
  • Option 1
  • Option 2
  • Option 2
  • TranslatableExists
  • Option 1
  • Option 2

Was this helpful?

  1. Usage

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

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

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

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

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

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')],
]);
PreviousPivot Model

Last updated 9 months ago

Was this helpful?