Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
Input Array is not being validated if it contains more than one value in laravel custom validation

Senario:

  • Title under each category will be unique.
  • I want to write a custom validator which cheks if same title with category exists.

Problem:

If I add one title it validates the uniqeness under same category. Such: It is working

But if I add multiple rows, validation does not work. Such: enter image description here

I think foreach in 'CategoryResolverTitle' file is being exicuted once. What is the solution. My Form

<label for="Title">Title</label>
<input type="text" name="names[]" class="form-control">

My Custom Validator:

<?php

namespace App\Rules;

use App\Models\QueryManagement\CategoryResolver;
use Illuminate\Contracts\Validation\Rule;

class CategoryTitle implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        foreach ($value as $element) {
            return !Category::whereDepartmentId(request('department'))->whereName($element)->exists();
        }
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'Title under same category must be unique';
    }
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You current passes() function seems to be faulty. It will only start the loop once, then return the result of the first check immediately. A better way would be:

foreach ($value as $element) {
   if(Category::whereDepartmentId(request('department'))->whereName($element)->exists()){
      return false;
   }
}
return true;
over 4 years ago · Santiago Trujillo Report

0

Asterisk symbol (*) is used to check values in the array, not the array itself.

$validator = Validator::make($request->all(), [
"names.*"  => "required|string|distinct|min:3",
]);

In the example above:

  • "names" must be an array with at least 3 elements,
  • values in the "names" array must be distinct (unique) strings, at least 3 characters long.

EDIT: Since Laravel 5.5 you can call validate() method directly on Request object like so:

$data = $request->validate([
"name.*"  => "required|string|distinct|min:3",
 ]);

also follow this url for more details

over 4 years ago · Santiago Trujillo Report

0

You can use array validation for this. There's a specific validation rule that can achieve database uniqueness:

$request->validate([
   "names.*" => Rule::unique('categories', 'name')->where(function ($q) {
         $q->where('department_id', request('department'));
    })
]);

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!