1

I have an array of elements that i want to validate, this array have this form

{
    "slugs" : {
        1 : "prueba",
        2 : "test"
}

Slugs is the input <input name="slugs[{{ $lang->id }}]"> and the numbers are the id of the language 1 = spanish / 2 = english.

What i want is to validate these field uniqueness, in a form request like this

public function rules()
{
    $rules = [
        //
        'slugs.*' => Rule::unique('translation_entries')->where(function($query) {
            //here i want to access the * that represent the lang id
            //like this $query->where('lang_id','=',$query->*);
        })
    ];
}

can i access the index * inside the unique Rule class?

1
  • What version of Laravel are you using? Commented Aug 28, 2018 at 21:34

1 Answer 1

1

As long as you're using Laravel >=5.5 you can use a closure

'slugs.*' => [
    function ($attribute, $value, $fail) {
        $id = str_after($attribute, '.');

        if (\DB::table('translation_entries')->where('lang_id', $id)->where('slug', $value)->exists()) {
            $fail('The slug has already been taken.');
        }
    },
],

Change 'slug' to be the name of the column on the translation_entries table (if different).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.