Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Use translator from validator in Can and Enum rules #49251

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/Illuminate/Validation/Rules/Can.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Illuminate\Validation\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Support\Facades\Gate;

class Can implements Rule
class Can implements Rule, ValidatorAwareRule
{
/**
* The ability to check.
Expand All @@ -21,6 +22,13 @@ class Can implements Rule
*/
protected $arguments;

/**
* The current validator instance.
*
* @var \Illuminate\Validation\Validator
*/
protected $validator;

/**
* Constructor.
*
Expand Down Expand Up @@ -56,10 +64,23 @@ public function passes($attribute, $value)
*/
public function message()
{
$message = trans('validation.can');
$message = $this->validator->getTranslator()->get('validation.can');

return $message === 'validation.can'
? ['The :attribute field contains an unauthorized value.']
: $message;
}

/**
* Set the current validator.
*
* @param \Illuminate\Validation\Validator $validator
* @return $this
*/
public function setValidator($validator)
{
$this->validator = $validator;

return $this;
}
}
25 changes: 23 additions & 2 deletions src/Illuminate/Validation/Rules/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Illuminate\Validation\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use TypeError;

class Enum implements Rule
class Enum implements Rule, ValidatorAwareRule
{
/**
* The type of the enum.
Expand All @@ -14,6 +15,13 @@ class Enum implements Rule
*/
protected $type;

/**
* The current validator instance.
*
* @var \Illuminate\Validation\Validator
*/
protected $validator;

/**
* Create a new rule instance.
*
Expand Down Expand Up @@ -56,10 +64,23 @@ public function passes($attribute, $value)
*/
public function message()
{
$message = trans('validation.enum');
$message = $this->validator->getTranslator()->get('validation.enum');

return $message === 'validation.enum'
? ['The selected :attribute is invalid.']
: $message;
}

/**
* Set the current validator.
*
* @param \Illuminate\Validation\Validator $validator
* @return $this
*/
public function setValidator($validator)
{
$this->validator = $validator;

return $this;
}
}