Code Monkey home page Code Monkey logo

Comments (18)

felixkiss avatar felixkiss commented on July 25, 2024

I can't reproduce the problem. It behaves correctly for me.

Can you give more specific details about your setup? (Laravel version, uniquewith version, code, etc.)

This is what I tried:

$rules = ['name' => 'required|unique_with:templates,organisation_id|min:2'];

$data = [
    'name' => 'test',
    'organisation_id' => 2,
];
dump(\Validator::make($data, $rules)->errors());

$data = [
    'name' => 'test',
    'organisation_id' => 1,
];
dump(\Validator::make($data, $rules)->errors());

Output (with 'test', 2 already in the database):

Illuminate\Support\MessageBag {#517
  #messages: array:1 [
    "name" => array:1 [
      0 => "This combination of name, organisation id already exists."
    ]
  ]
  #format: ":message"
}
Illuminate\Support\MessageBag {#519
  #messages: []
  #format: ":message"
}

from uniquewith-validator.

CrixuAMG avatar CrixuAMG commented on July 25, 2024

The organisation_id is part of the user present in $request->user(), it is not being sent as form data.
Using latest stable versions of all packages.

from uniquewith-validator.

CrixuAMG avatar CrixuAMG commented on July 25, 2024

I've changed the rules to be the following:

$rules = [
            'name' => 'required|unique_with:templates,organisation_id|min:2'
];
$this->merge(['organisation_id' => $this->user()->organisation_id]);

return $rules;

But only this is executed in the query:
select count(*) as aggregate from 'templates' where 'name' = 'test';

from uniquewith-validator.

felixkiss avatar felixkiss commented on July 25, 2024

Are you using a FormRequest object? What is this merge method you are calling?

from uniquewith-validator.

CrixuAMG avatar CrixuAMG commented on July 25, 2024

I've created a request class using php artisan make:request RequestName.
This creates a class that extends the FormRequest class by default, and has been some time in Laravel ;)
The merge method I'm using is the exact same as having Request $request as a parameter in a method, and then calling $request->merge(['some data' => 'some value']);

from uniquewith-validator.

felixkiss avatar felixkiss commented on July 25, 2024

Still having issues or can this issue be closed? It worked in my example

from uniquewith-validator.

CrixuAMG avatar CrixuAMG commented on July 25, 2024

The issue is still here.
When doing what I said in my original post:

I've added a rule:
'name' => 'required|unique_with:templates,organisation_id|min:2'
And I have data:
name='test', organisation_id=2
And when I try to add name='test' with organisation_id 1, it fails.

Still fails.

Error message: This combination of name, organisation id already exists.

from uniquewith-validator.

tonnevillec avatar tonnevillec commented on July 25, 2024

Hi,
I have the same problem but in reverse.
I can rename an organization while another exists and already has the name passed as a parameter.

from uniquewith-validator.

felixkiss avatar felixkiss commented on July 25, 2024

@tonnevillec please open a new issue with more details and code that shows how you use the library so I can reproduce or advise

from uniquewith-validator.

tonnevillec avatar tonnevillec commented on July 25, 2024

This is exactly the same problem.
I have a table client with 'id|name|owner_id' and for one owner i couldn't have two client with the same name.
So i try to validate with:
'name' => 'required|unique_with:client,owner_id'

I'm sorry, I don't know what I can give as other information because it is a very simple case.

from uniquewith-validator.

felixkiss avatar felixkiss commented on July 25, 2024

@tonnevillec

  • What version of Laravel and uniquewith-validator do you use?
  • Please post output of this php artisan tinker snippet:
DB::table('client')->insert(['name' => 'foo-bar', 'owner_id' => 1]);
Validator::make(['name' => 'foo-bar', 'owner_id' => 1], ['name' => 'unique_with:client,owner_id'])->errors();
Validator::make(['name' => 'foo-bar', 'owner_id' => 2], ['name' => 'unique_with:client,owner_id'])->errors();

from uniquewith-validator.

tonnevillec avatar tonnevillec commented on July 25, 2024

Laravel 5.2.45
uniquewith 3.0.0

DB::table('client')->insert(['name'=>'foo-bar', 'owner_id'=>2544]); => true Validator::make(['name'=>'foo-bar', 'owner_id'=>2544], ['name'=>'unique_with:client,owner_id'])->errors(); => Illuminate\Support\MessageBag {#945} Validator::make(['name'=>'essai', 'owner_id'=>2544], ['name'=>'unique_with:client,owner_id'])->errors(); => Illuminate\Support\MessageBag {#946}

I don't know if it can help. The second test with 'essai' should normaly return an error...

from uniquewith-validator.

CrixuAMG avatar CrixuAMG commented on July 25, 2024

@tonnevillec I would expect foo-bar to be the one causing an error, since you specify the name should be unique in combination with the owner id.
Can you dump what's inside the MessageBag?

from uniquewith-validator.

tonnevillec avatar tonnevillec commented on July 25, 2024

... in the messageBag i have the error as expected.
But in my api it doesn't work.

$this->validate($request, [ 'name' => 'required|unique_with:client,owner_id|min:2' ]);

from uniquewith-validator.

tonnevillec avatar tonnevillec commented on July 25, 2024

sorry i found my mistake and it does not depend on uniquewith

from uniquewith-validator.

CrixuAMG avatar CrixuAMG commented on July 25, 2024

Can you elaborate on what went wrong?

from uniquewith-validator.

tonnevillec avatar tonnevillec commented on July 25, 2024

Just an error in the call of my owner_id so when I wanted to test the uniqueness of the name for the owner I always had a return "ok" because the couple name / owner did not necessarily exist.

from uniquewith-validator.

CrixuAMG avatar CrixuAMG commented on July 25, 2024

Ok, thanks for the information 👍

from uniquewith-validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.