Skip to content

Commit

Permalink
Merge pull request #687 from hydephp/replace-validation-placeholders
Browse files Browse the repository at this point in the history
Replace validation placeholders
  • Loading branch information
caendesilva authored Nov 24, 2022
2 parents a427943 + 0d19ac0 commit 6c93614
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
96 changes: 95 additions & 1 deletion packages/framework/src/Console/Concerns/ValidatingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace Hyde\Console\Concerns;

use function array_keys;
use function array_values;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use LaravelZero\Framework\Commands\Command;
use RuntimeException;
use function str_replace;
use function ucfirst;

/**
Expand Down Expand Up @@ -51,7 +55,7 @@ public function askWithValidation(
}

foreach ($validator->errors()->all() as $error) {
$this->error($error);
$this->error($this->translate($name, $error));
}

$retryCount++;
Expand All @@ -63,4 +67,94 @@ public function askWithValidation(

return $this->askWithValidation($name, $question, $rules, null, $retryCount);
}

protected function translate($name, string $error): string
{
return $this->makeReplacements($name, Str::after($error, 'validation.'), $this->getTranslationLines());
}

protected function makeReplacements(string $name, string $line, array $replace): string
{
return str_replace(':attribute', $name, str_replace(array_keys($replace), array_values($replace), $line));
}

protected function getTranslationLines(): array
{
return [
'accepted' => 'The :attribute must be accepted.',
'accepted_if' => 'The :attribute must be accepted when :other is :value.',
'active_url' => 'The :attribute is not a valid URL.',
'after' => 'The :attribute must be a date after :date.',
'after_or_equal' => 'The :attribute must be a date after or equal to :date.',
'alpha' => 'The :attribute must only contain letters.',
'alpha_dash' => 'The :attribute must only contain letters, numbers, dashes and underscores.',
'alpha_num' => 'The :attribute must only contain letters and numbers.',
'array' => 'The :attribute must be an array.',
'before' => 'The :attribute must be a date before :date.',
'before_or_equal' => 'The :attribute must be a date before or equal to :date.',
'boolean' => 'The :attribute field must be true or false.',
'confirmed' => 'The :attribute confirmation does not match.',
'current_password' => 'The password is incorrect.',
'date' => 'The :attribute is not a valid date.',
'date_equals' => 'The :attribute must be a date equal to :date.',
'date_format' => 'The :attribute does not match the format :format.',
'declined' => 'The :attribute must be declined.',
'declined_if' => 'The :attribute must be declined when :other is :value.',
'different' => 'The :attribute and :other must be different.',
'digits' => 'The :attribute must be :digits digits.',
'digits_between' => 'The :attribute must be between :min and :max digits.',
'dimensions' => 'The :attribute has invalid image dimensions.',
'distinct' => 'The :attribute field has a duplicate value.',
'doesnt_end_with' => 'The :attribute may not end with one of the following: :values.',
'doesnt_start_with' => 'The :attribute may not start with one of the following: :values.',
'email' => 'The :attribute must be a valid email address.',
'ends_with' => 'The :attribute must end with one of the following: :values.',
'enum' => 'The selected :attribute is invalid.',
'exists' => 'The selected :attribute is invalid.',
'file' => 'The :attribute must be a file.',
'filled' => 'The :attribute field must have a value.',
'image' => 'The :attribute must be an image.',
'in' => 'The selected :attribute is invalid.',
'in_array' => 'The :attribute field does not exist in :other.',
'integer' => 'The :attribute must be an integer.',
'ip' => 'The :attribute must be a valid IP address.',
'ipv4' => 'The :attribute must be a valid IPv4 address.',
'ipv6' => 'The :attribute must be a valid IPv6 address.',
'json' => 'The :attribute must be a valid JSON string.',
'lowercase' => 'The :attribute must be lowercase.',
'mac_address' => 'The :attribute must be a valid MAC address.',
'max_digits' => 'The :attribute must not have more than :max digits.',
'mimes' => 'The :attribute must be a file of type: :values.',
'mimetypes' => 'The :attribute must be a file of type: :values.',
'min_digits' => 'The :attribute must have at least :min digits.',
'multiple_of' => 'The :attribute must be a multiple of :value.',
'not_in' => 'The selected :attribute is invalid.',
'not_regex' => 'The :attribute format is invalid.',
'numeric' => 'The :attribute must be a number.',
'present' => 'The :attribute field must be present.',
'prohibited' => 'The :attribute field is prohibited.',
'prohibited_if' => 'The :attribute field is prohibited when :other is :value.',
'prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.',
'prohibits' => 'The :attribute field prohibits :other from being present.',
'regex' => 'The :attribute format is invalid.',
'required' => 'The :attribute field is required.',
'required_array_keys' => 'The :attribute field must contain entries for: :values.',
'required_if' => 'The :attribute field is required when :other is :value.',
'required_if_accepted' => 'The :attribute field is required when :other is accepted.',
'required_unless' => 'The :attribute field is required unless :other is in :values.',
'required_with' => 'The :attribute field is required when :values is present.',
'required_with_all' => 'The :attribute field is required when :values are present.',
'required_without' => 'The :attribute field is required when :values is not present.',
'required_without_all' => 'The :attribute field is required when none of :values are present.',
'same' => 'The :attribute and :other must match.',
'starts_with' => 'The :attribute must start with one of the following: :values.',
'string' => 'The :attribute must be a string.',
'timezone' => 'The :attribute must be a valid timezone.',
'unique' => 'The :attribute has already been taken.',
'uploaded' => 'The :attribute failed to upload.',
'uppercase' => 'The :attribute must be uppercase.',
'url' => 'The :attribute must be a valid URL.',
'uuid' => 'The :attribute must be a valid UUID.',
];
}
}
4 changes: 2 additions & 2 deletions packages/framework/tests/Feature/ValidatingCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testAskWithValidationRetries()
})->andReturn('', 'Jane Doe');

$output->shouldReceive('writeln')->times(1)->withArgs(function (string $message) {
return $message === '<error>validation.required</error>';
return $message === '<error>The name field is required.</error>';
});

$output->shouldReceive('writeln')->once()->withArgs(function (string $message) {
Expand All @@ -66,7 +66,7 @@ public function testAskWithValidationRetriesTooManyTimes()
})->andReturn('');

$output->shouldReceive('writeln')->times(30)->withArgs(function (string $message) {
return $message === '<error>validation.required</error>';
return $message === '<error>The name field is required.</error>';
});

$this->expectException(RuntimeException::class);
Expand Down

0 comments on commit 6c93614

Please sign in to comment.