Comments (18)
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.
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.
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.
Are you using a FormRequest
object? What is this merge
method you are calling?
from uniquewith-validator.
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.
Still having issues or can this issue be closed? It worked in my example
from uniquewith-validator.
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.
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.
@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.
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.
- 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.
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.
@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.
... 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.
sorry i found my mistake and it does not depend on uniquewith
from uniquewith-validator.
Can you elaborate on what went wrong?
from uniquewith-validator.
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.
Ok, thanks for the information 👍
from uniquewith-validator.
Related Issues (20)
- It only works for the current request HOT 4
- Bad method call HOT 1
- Validation fails when updating composite keys with ignore HOT 1
- Using that validation rule with renamed fileds
- Update for Laravel 6.0 HOT 1
- Can't seem to get it working for Polymorph fields HOT 2
- How to add deleted_at, NULL HOT 2
- Update for Laravel 8.0 HOT 10
- Please add support for PHP 8 HOT 1
- Ignore is not working in array validation HOT 1
- unique_with can't validation on multiple column
- Migrate test suite from Travis to Github Actions HOT 1
- Wrong locale set on app
- Compatibility Issue with laravel 9 php 8.X HOT 11
- Counter-intuitive deleted_at in rules
- Ignoring deleted items not working properly
- Laravel 5.4 : doesn't work on updating ! HOT 5
- its not working with database connection HOT 1
- Giving duplicate entry in every new entry HOT 2
- Is there a possibility of a validator plugin for exist_with? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uniquewith-validator.