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

Custom Validator - can't set error message #1895

Closed
edalzell opened this Issue Apr 23, 2018 · 0 comments

Comments

Projects
None yet
3 participants
@edalzell

edalzell commented Apr 23, 2018

I've created a custom validator in the Service Provider's boot method, and I can validate against it:

        Validator::extend('phone_number ', function ($attribute, $value, $parameters, $validator) {
            try {
                PhoneNumberUtil::getInstance()->parse($value, 'US');
            } catch (NumberParseException $e) {
                return false;
            }

            return true;
        });

I have a validation.php in resources/lang/en/ with

<?php
return [
    'phone_number' => 'Foo bar',
  ];

When the validation fails, that message is not displayed, instead you just see validation.phone_number as the error message.

If I try to pass in an error message to the extend function like so:

        Validator::extend('phone_number ', function ($attribute, $value, $parameters, $validator) {
            try {
                PhoneNumberUtil::getInstance()->parse($value, 'US');
            } catch (NumberParseException $e) {
                return false;
            }

            return true;
        }, $this->trans('validation.phone_number'));

When the validation fails you see: addons.Phone::validation.phone_number

Is there another way to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment