Guest User

Untitled

a guest
Sep 26th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. // Here we're just overriding the "getMessage()" function from Laravel 5.6 to accept translation choices.
  2.  
  3. <?php
  4. namespace App\Services;
  5.  
  6. use Illuminate\Validation\Validator;
  7. use Illuminate\Support\Str;
  8.  
  9. class GenderizedValidator extends Validator {
  10. protected function getMessage($attribute, $rule)
  11. {
  12. $inlineMessage = $this->getFromLocalArray(
  13. $attribute, $lowerRule = Str::snake($rule)
  14. );
  15.  
  16. // First we will retrieve the custom message for the validation rule if one
  17. // exists. If a custom validation message is being used we'll return the
  18. // custom message, otherwise we'll keep searching for a valid message.
  19. if (! is_null($inlineMessage)) {
  20. return $inlineMessage;
  21. }
  22.  
  23. $customMessage = $this->getCustomMessageFromTranslator(
  24. $customKey = "validation.custom.{$attribute}.{$lowerRule}"
  25. );
  26.  
  27. // First we check for a custom defined validation message for the attribute
  28. // and rule. This allows the developer to specify specific messages for
  29. // only some attributes and rules that need to get specially formed.
  30. if ($customMessage !== $customKey) {
  31. return $customMessage;
  32. }
  33.  
  34. // If the rule being validated is a "size" rule, we will need to gather the
  35. // specific error message for the type of attribute being validated such
  36. // as a number, file or string which all have different message types.
  37. elseif (in_array($rule, $this->sizeRules)) {
  38. return $this->getSizeMessage($attribute, $rule);
  39. }
  40.  
  41. // Finally, if no developer specified messages have been set, and no other
  42. // special messages apply for this rule, we will just pull the default
  43. // messages out of the translator service for this validation rule.
  44. $key = "validation.{$lowerRule}";
  45.  
  46. $choice = trans('validation.attribute_gender.' . $attribute) == 'f' ? 1 : 0;
  47.  
  48. if ($key != ($value = $this->translator->transChoice($key, $choice))) {
  49. return $value;
  50. }
  51.  
  52. return $this->getFromLocalArray(
  53. $attribute, $lowerRule, $this->fallbackMessages
  54. ) ?: $key;
  55. }
  56. }
Add Comment
Please, Sign In to add comment