Merge pull request #13914 from snipe/bug/sc-24073

Fixed missing translation string for `validation.two_column_unique_undeleted`
This commit is contained in:
snipe 2023-11-21 15:44:56 +00:00 committed by GitHub
commit 9502525a41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View file

@ -81,8 +81,19 @@ class ValidationServiceProvider extends ServiceProvider
} }
}); });
// Unique if undeleted for two columns /**
// Same as unique_undeleted but taking the combination of two columns as unique constrain. * 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.
*
* $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) { Validator::extend('two_column_unique_undeleted', function ($attribute, $value, $parameters, $validator) {
if (count($parameters)) { if (count($parameters)) {
$count = DB::table($parameters[0]) $count = DB::table($parameters[0])
@ -96,6 +107,27 @@ 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."
*
* 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);
$message = str_replace(':table2', $parameters[2], $message);
// Change underscores to spaces for a friendlier display
$message = str_replace('_', ' ', $message);
return $message;
});
// Prevent circular references // Prevent circular references
// //
// Example usage in Location model where parent_id references another Location: // Example usage in Location model where parent_id references another Location:

View file

@ -90,6 +90,7 @@ return [
], ],
'string' => 'The :attribute must be a string.', 'string' => 'The :attribute must be a string.',
'timezone' => 'The :attribute must be a valid zone.', 'timezone' => 'The :attribute must be a valid zone.',
'two_column_unique_undeleted' => 'The :attribute must be unique across :table1 and :table2. ',
'unique' => 'The :attribute has already been taken.', 'unique' => 'The :attribute has already been taken.',
'uploaded' => 'The :attribute failed to upload.', 'uploaded' => 'The :attribute failed to upload.',
'url' => 'The :attribute format is invalid.', 'url' => 'The :attribute format is invalid.',