0

I have a Product model with a text input field for the product number. In my Laravel application I validate this field to be unique to that specific user. So two users can have a same product number, but one user cannot have duplicate. So far the validation rules work when adding new products:

'product_no' => 'nullable|unique:products,product_no,NULL,id,user_id,' . auth()->user()->id

However, when editing the same product, the validation fails. Probably because it already exists. I am not sure how to exclude the existing ID in the validation. Any ideas?

Ashok Kumar Jayaraman
  • 2,887
  • 2
  • 32
  • 40
rebellion
  • 6,628
  • 11
  • 48
  • 79
  • usually, i used two different validator expression for new row and existing one. but i hope someone can do something better. – Bagus Tesa Oct 20 '18 at 06:28
  • Why not use the array notation for validation and add the unique key in the non-update case – online Thomas Oct 20 '18 at 06:36
  • @BagusTesa How do I differentiate the form methods? – rebellion Oct 20 '18 at 07:04
  • @ThomasMoors Example? – rebellion Oct 20 '18 at 07:04
  • @Ronny-AndréBendiksen, when using [Resource Controller](https://laravel.com/docs/master/controllers#resource-controllers), you had two methods, one for storing the other one for updating. Thomas approach would to use the validation rule this way `$validationRule = ['product_no' => ['nullable', Rule::unique('products', 'product_no')->ignore(auth()->user()->id, 'user_id')]]`. i havent tried Thomas' suggestion, just a rough guess.. it seems new feature. – Bagus Tesa Oct 20 '18 at 07:18
  • @BagusTesa Ah you mean putting it in the controller? I could do that, but I was hoping to put it in the Form Request. – rebellion Oct 20 '18 at 11:08
  • form request? can you elaborate? – Bagus Tesa Oct 20 '18 at 11:10

1 Answers1

0

Example as requested

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class Request1 extends FormRequest
{
    private $rules;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function __construct()
    {
         parent::__construct();
         $this->rules = [
            'password' => [
                'nullable',
            ]
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return $this->rules;
    }
}

And the one with unique looks like this then

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class Request2 extends Request1
{

    public function __construct()
    {
         parent::__construct();
         $this->rules[] = 'unique:products,product_no,NULL,id,user_id,' . auth()->user()->id';  
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return $this->rules;
    }
}
online Thomas
  • 8,864
  • 6
  • 44
  • 85