Skip to content

Do not show the validation error message in pipe syntax #133

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

Closed
Daniyal-Javani opened this issue Mar 18, 2020 · 2 comments
Closed

Do not show the validation error message in pipe syntax #133

Daniyal-Javani opened this issue Mar 18, 2020 · 2 comments

Comments

@Daniyal-Javani
Copy link
Contributor

Daniyal-Javani commented Mar 18, 2020

The following code works fine:
'status' => ['required', new EnumKey(OrderStatus::class)]
and returns this
"The key you have entered is invalid."

But when we use the pipe syntax like this:
'status' => 'required|enum_key:' . OrderStatus::class
it returns:
"validation.enum_key"

If it has intended to fixed, here are my suggestions
The easiest one:
Copy and paste the string like this

Validator::extend('enum_key', function ($attribute, $value, $parameters, $validator) {
    $enum = $parameters[0] ?? null;

    return (new EnumKey($enum))->passes($attribute, $value);
}, 'The key you have entered is invalid.');

Or do some messy things like

Validator::extend('enum_key', function ($attribute, $value, $parameters, $validator) {
    $enum = $parameters[0] ?? null;

    return (new EnumKey($enum))->passes($attribute, $value);
}, ((new \ReflectionClass(EnumKey::class))->newInstanceWithoutConstructor())->message());

Please let me know your idea about the solution of the issue.

@BenSampo
Copy link
Owner

We should probably be using translation files for both.

See
https://laravel.com/docs/master/packages#translations
and
https://laracasts.com/discuss/channels/laravel/package-development-custom-validation-messages?page=1

@Daniyal-Javani Daniyal-Javani mentioned this issue Mar 19, 2020
3 tasks
@BenSampo
Copy link
Owner

Resolved by #134

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants