New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique_with does not work?? #79

Open
CrixuAMG opened this Issue Aug 10, 2017 · 18 comments

Comments

Projects
None yet
3 participants
@CrixuAMG

CrixuAMG commented Aug 10, 2017

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.

@felixkiss

This comment has been minimized.

Owner

felixkiss commented Aug 10, 2017

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"
}
@CrixuAMG

This comment has been minimized.

CrixuAMG commented Aug 11, 2017

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.

@CrixuAMG

This comment has been minimized.

CrixuAMG commented Aug 11, 2017

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';

@felixkiss

This comment has been minimized.

Owner

felixkiss commented Aug 14, 2017

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

@CrixuAMG

This comment has been minimized.

CrixuAMG commented Aug 15, 2017

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']);

@felixkiss

This comment has been minimized.

Owner

felixkiss commented Aug 26, 2017

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

@felixkiss felixkiss added the question label Aug 26, 2017

@CrixuAMG

This comment has been minimized.

CrixuAMG commented Aug 28, 2017

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.

@tonnevillec

This comment has been minimized.

tonnevillec commented Nov 14, 2017

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.

@felixkiss

This comment has been minimized.

Owner

felixkiss commented Nov 14, 2017

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

@tonnevillec

This comment has been minimized.

tonnevillec commented Nov 14, 2017

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.

@felixkiss

This comment has been minimized.

Owner

felixkiss commented Nov 14, 2017

@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();
@tonnevillec

This comment has been minimized.

tonnevillec commented Nov 15, 2017

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...

@CrixuAMG

This comment has been minimized.

CrixuAMG commented Nov 15, 2017

@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?

@tonnevillec

This comment has been minimized.

tonnevillec commented Nov 15, 2017

... 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' ]);

@tonnevillec

This comment has been minimized.

tonnevillec commented Nov 15, 2017

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

@CrixuAMG

This comment has been minimized.

CrixuAMG commented Nov 15, 2017

Can you elaborate on what went wrong?

@tonnevillec

This comment has been minimized.

tonnevillec commented Nov 15, 2017

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.

@CrixuAMG

This comment has been minimized.

CrixuAMG commented Nov 15, 2017

Ok, thanks for the information 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment