diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 477b35b7df..2909f97aa5 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -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', ]; diff --git a/app/Models/Category.php b/app/Models/Category.php index 0cd4f84d39..d86a155495 100755 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -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', ); diff --git a/app/Models/Company.php b/app/Models/Company.php index c63b6134b9..17d52b9d7d 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -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 diff --git a/app/Models/Location.php b/app/Models/Location.php index a6767eca5d..43d9d591cb 100755 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -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', diff --git a/app/Models/Statuslabel.php b/app/Models/Statuslabel.php index d99f8df2cc..cc331a54e7 100755 --- a/app/Models/Statuslabel.php +++ b/app/Models/Statuslabel.php @@ -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', diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php index bbaac8953d..254e49b190 100755 --- a/app/Models/Supplier.php +++ b/app/Models/Supplier.php @@ -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', diff --git a/app/Models/User.php b/app/Models/User.php index 762153d033..ace1351feb 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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', ]; diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 99a492ba85..4617a50e58 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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; + + + }); } /** diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index 2d4fda6f7a..03c7471cab 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -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.", /*