@addison
To validate a Persian slug in Laravel, you can create a custom validation rule using the following steps:
1
|
php artisan make:rule PersianSlug |
This will generate a new file in the app/Rules
directory called PersianSlug.php
.
1 2 3 4 |
public function passes($attribute, $value) { return preg_match('/^[p{L}p{N}]+(?:[-][p{L}p{N}]+)*$/', $value); } |
This code uses a regular expression pattern to match Persian slugs. The pattern allows for Persian letters and numbers, as well as hyphens in between words.
1 2 3 4 |
public function message() { return 'The :attribute field must be a valid Persian slug.'; } |
1 2 3 4 5 |
use AppRulesPersianSlug; $validatedData = $request->validate([ 'slug' => ['required', 'string', new PersianSlug], ]); |
With these steps, you should now have a custom validation rule for validating Persian slugs in Laravel.