From 0ae91d305d624a9cae6ab661db7d295ac6daa653 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 21 Nov 2023 15:03:03 +0000 Subject: [PATCH 1/5] Add the two_column_unique_undeleted translation Signed-off-by: snipe --- resources/lang/en/validation.php | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index 85e844781..6bceae044 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -90,6 +90,7 @@ return [ ], 'string' => 'The :attribute must be a string.', 'timezone' => 'The :attribute must be a valid zone.', + 'two_column_unique_undeleted' => 'The :attribute value must be unique across :table1 and :table2. ', 'unique' => 'The :attribute has already been taken.', 'uploaded' => 'The :attribute failed to upload.', 'url' => 'The :attribute format is invalid.', From 590cd0c71f6a2739bd3ec31dabb5d51415173a36 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 21 Nov 2023 15:03:59 +0000 Subject: [PATCH 2/5] Added Validator::replace() for friendlier interpretation of validation string Signed-off-by: snipe --- app/Providers/ValidationServiceProvider.php | 27 +++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php index 5ee904d20..0212c9faa 100644 --- a/app/Providers/ValidationServiceProvider.php +++ b/app/Providers/ValidationServiceProvider.php @@ -80,9 +80,14 @@ class ValidationServiceProvider extends ServiceProvider return $count < 1; } }); + + /** + * Unique if undeleted for two columns + * + * Same as unique_undeleted but taking the combination of two columns as unique constrain. + * This uses the Validator::replacer('two_column_unique_undeleted') below for nicer translations. + */ - // Unique if undeleted for two columns - // Same as unique_undeleted but taking the combination of two columns as unique constrain. Validator::extend('two_column_unique_undeleted', function ($attribute, $value, $parameters, $validator) { if (count($parameters)) { $count = DB::table($parameters[0]) @@ -96,6 +101,24 @@ class ValidationServiceProvider extends ServiceProvider }); + /** + * This is the validator replace static method that allows us to pass the $parameters of the table names + * into the translation string in validation.two_column_unique_undeleted for two_column_unique_undeleted + * validation messages. + * + * This is invoked automatically by Validator::extend('two_column_unique_undeleted') above and + * produces a translation like: "The name value must be unique across categories and category type." + */ + Validator::replacer('two_column_unique_undeleted', function($message, $attribute, $rule, $parameters) { + $message = str_replace(':table1', $parameters[0], $message); + $message = str_replace(':table2', $parameters[2], $message); + + // Change underscores to spaces for a friendlier display + $message = str_replace('_', ' ', $message); + return $message; + }); + + // Prevent circular references // // Example usage in Location model where parent_id references another Location: From fb125af0df7f7fe1364c67cb50ae5736fe683ced Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 21 Nov 2023 15:09:50 +0000 Subject: [PATCH 3/5] Tweaked language Signed-off-by: snipe --- resources/lang/en/validation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index 6bceae044..57e354f07 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -90,7 +90,7 @@ return [ ], 'string' => 'The :attribute must be a string.', 'timezone' => 'The :attribute must be a valid zone.', - 'two_column_unique_undeleted' => 'The :attribute value must be unique across :table1 and :table2. ', + 'two_column_unique_undeleted' => 'The :attribute must be unique across :table1 and :table2. ', 'unique' => 'The :attribute has already been taken.', 'uploaded' => 'The :attribute failed to upload.', 'url' => 'The :attribute format is invalid.', From 6a2ab2cfb285cebc2479582e6cbe74116f3eb92b Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 21 Nov 2023 15:10:19 +0000 Subject: [PATCH 4/5] Removed line break Signed-off-by: snipe --- app/Providers/ValidationServiceProvider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php index 0212c9faa..2f0a3f5e5 100644 --- a/app/Providers/ValidationServiceProvider.php +++ b/app/Providers/ValidationServiceProvider.php @@ -87,7 +87,6 @@ class ValidationServiceProvider extends ServiceProvider * Same as unique_undeleted but taking the combination of two columns as unique constrain. * This uses the Validator::replacer('two_column_unique_undeleted') below for nicer translations. */ - Validator::extend('two_column_unique_undeleted', function ($attribute, $value, $parameters, $validator) { if (count($parameters)) { $count = DB::table($parameters[0]) From 68ac1aaae0404b4f16a7680b8718856d1baf4f89 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 21 Nov 2023 15:36:11 +0000 Subject: [PATCH 5/5] Additional comments Signed-off-by: snipe --- app/Providers/ValidationServiceProvider.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php index 2f0a3f5e5..50468c8d7 100644 --- a/app/Providers/ValidationServiceProvider.php +++ b/app/Providers/ValidationServiceProvider.php @@ -86,6 +86,13 @@ class ValidationServiceProvider extends ServiceProvider * * Same as unique_undeleted but taking the combination of two columns as unique constrain. * This uses the Validator::replacer('two_column_unique_undeleted') below for nicer translations. + * + * $parameters[0] - the name of the first table we're looking at + * $parameters[1] - the ID (this will be 0 on new creations) + * $parameters[2] - the name of the second table we're looking at + * $parameters[3] - the value that the request is passing for the second table we're + * checking for uniqueness across + * */ Validator::extend('two_column_unique_undeleted', function ($attribute, $value, $parameters, $validator) { if (count($parameters)) { @@ -107,6 +114,9 @@ class ValidationServiceProvider extends ServiceProvider * * This is invoked automatically by Validator::extend('two_column_unique_undeleted') above and * produces a translation like: "The name value must be unique across categories and category type." + * + * The $parameters passed coincide with the ones the two_column_unique_undeleted custom validator above + * uses, so $parameter[0] is the first table and so $parameter[2] is the second table. */ Validator::replacer('two_column_unique_undeleted', function($message, $attribute, $rule, $parameters) { $message = str_replace(':table1', $parameters[0], $message);