如何在laravel 5中向服务提供者的错误消息添加错误

i added a custom validation rule in a service provider like this

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ValidatorServiceProvider extends ServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot() {
        $this->app['validator']->extend('chk', function ($attribute, $value, $parameters) {
            if(some_check){
                return true;
            }else{
                //add error message
            }
        });
    }

    public function register() {

    }

}

but i want it to add an error message instead of returning false, how to do that ?

extend() can take 3 parameters, the first being the validation name, the second being the function which contains all the data to validate and the third which allows for an error message, so you would do it the following way:

$this->app['validator']->extend('chk', function ($attribute, $value, $parameters) {
    if (some_check) {
        return true;
    } else {
        return false;
    }
 }, ':attribute field error.');

Validation requires to return boolean, true will pass the validation and false will fire the error message. And the :attribute is the placeholder for the field name. You can also add the custom error message to the resources/lang/en/validation.php file too. So not only keeping them contained in one place, it also benefits you if you have various language options for your application.