Custom validator for multiple deleted items with the same unique field

This commit is contained in:
snipe 2016-07-26 00:44:26 -07:00
parent 987b969e88
commit a44b90dfc9
9 changed files with 29 additions and 7 deletions

View file

@ -51,7 +51,7 @@ class Asset extends Depreciable
'checkout_date' => 'date|max:10|min:10',
'checkin_date' => 'date|max:10|min:10',
'supplier_id' => 'integer',
'asset_tag' => 'required|min:1|max:255|unique:assets,asset_tag,NULL,deleted_at',
'asset_tag' => 'required|min:1|max:255|unique_undeleted:assets',
'status' => 'integer',
];

View file

@ -25,7 +25,7 @@ class Category extends Model
*/
public $rules = array(
'user_id' => 'numeric',
'name' => 'required|min:1|max:255|unique:categories,name,NULL,deleted_at',
'name' => 'required|min:1|max:255|unique_undeleted:categories',
'category_type' => 'required',
);

View file

@ -16,7 +16,11 @@ final class Company extends Model
protected $table = 'companies';
// Declare the rules for the model validation
protected $rules = ['name' => 'required|min:1|max:255|unique:companies,name'];
protected $rules = [
'name' => 'required|min:1|max:255|unique_undeleted:companies'
];
/**
* Whether the model should inject it's identifier to the unique
* validation rules before attempting validation. If this property

View file

@ -13,7 +13,7 @@ class Location extends Model
protected $dates = ['deleted_at'];
protected $table = 'locations';
protected $rules = array(
'name' => 'required|min:3|max:255|unique:locations,name,NULL,deleted_at',
'name' => 'required|min:3|max:255|unique_undeleted:locations',
'city' => 'min:3|max:255',
'state' => 'min:2|max:32',
'country' => 'min:2|max:2|max:2',

View file

@ -16,7 +16,7 @@ class Statuslabel extends Model
protected $rules = array(
'name' => 'required|string|unique:status_labels,name,NULL,deleted_at',
'name' => 'required|string|unique_undeleted:status_labels',
'notes' => 'string',
'deployable' => 'required',
'pending' => 'required',

View file

@ -11,7 +11,7 @@ class Supplier extends Model
protected $dates = ['deleted_at'];
protected $rules = array(
'name' => 'required|min:3|max:255|unique:suppliers,name,NULL,deleted_at',
'name' => 'required|min:3|max:255|unique_undeleted:suppliers',
'address' => 'min:3|max:255',
'address2' => 'min:2|max:255',
'city' => 'min:3|max:255',

View file

@ -32,7 +32,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
protected $rules = [
'first_name' => 'required|string|min:1',
'username' => 'required|string|min:2|unique:users,username,NULL,deleted_at',
'username' => 'required|string|min:2|unique_undeleted:users',
'email' => 'email',
'password' => 'required|min:6',
];

View file

@ -3,6 +3,8 @@ namespace App\Providers;
use Validator;
use Illuminate\Support\ServiceProvider;
use Log;
use DB;
/**
* This service provider handles a few custom validation rules.
@ -47,6 +49,21 @@ class AppServiceProvider extends ServiceProvider
}
});
// Unique only if undeleted
// This works around the use case where multiple deleted items have the same unique attribute.
// (I think this is a bug in Laravel's validator?)
Validator::extend('unique_undeleted', function($attribute, $value, $parameters, $validator) {
$count = DB::table($parameters[0])->where($attribute,'=',$value)->whereNull('deleted_at')->count();
if ($count < 1) {
return true;
}
return false;
});
}
/**

View file

@ -65,6 +65,7 @@ return array(
"unique" => "The :attribute has already been taken.",
"url" => "The :attribute format is invalid.",
"statuslabel_type" => "You must select a valid status label type",
"unique_undeleted" => "The :attribute must be unique.",
/*