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

Fix validation extensions #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/Rules/VatNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@

class VatNumber implements ValidationRule
{
public function isValid(mixed $value): bool
{
return VatValidator::validate($value);
}

public function validate(string $attribute, mixed $value, \Closure $fail): void
{
if (! VatValidator::validate($value)) {
if (! $this->isValid($value)) {
$fail(__('The :attribute must be a valid VAT number.'));
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Rules/VatNumberExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

class VatNumberExist implements ValidationRule
{
public function isValid(mixed $value): bool
{
return VatValidator::validateExistence($value);
}

public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (! VatValidator::validateExistence($value)) {
if (! $this->isValid($value)) {
$fail(__('VAT number :attribute not exist.'));
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Rules/VatNumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@

class VatNumberFormat implements ValidationRule
{
public function isValid(mixed $value): bool
{
return VatValidator::validateFormat($value);
}

public function validate(string $attribute, mixed $value, \Closure $fail): void
{
if (! VatValidator::validateFormat($value)) {
if (! $this->isValid($value)) {
$fail(__('The :attribute must be write in a valid number format {country_name}{vat_number}.'));
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/VatValidatorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ public function boot(): void
/**
* Register the "vat_number" validation rule.
*/
Validator::extend('vat_number', static function ($attribute, $value, $parameters, $validator): void {
Validator::extend('vat_number', static function ($attribute, $value, $parameters, $validator): bool {
$rule = new VatNumber();
$rule->validate($attribute, $value, static fn (string $message = null): null => null);
return $rule->isValid($value);
});

/**
* Register the "vat_number_exist" validation rule.
*/
Validator::extend('vat_number_exist', static function ($attribute, $value, $parameters, $validator): void {
Validator::extend('vat_number_exist', static function ($attribute, $value, $parameters, $validator): bool {
$rule = new VatNumberExist();
$rule->validate($attribute, $value, static fn (string $message = null): null => null);
return $rule->isValid($value);
});

/**
* Register the "vat_number_format" validation rule.
*/
Validator::extend('vat_number_format', static function ($attribute, $value, $parameters, $validator): void {
Validator::extend('vat_number_format', static function ($attribute, $value, $parameters, $validator): bool {
$rule = new VatNumberFormat();
$rule->validate($attribute, $value, static fn (string $message = null): null => null);
return $rule->isValid($value);
});
}

Expand Down