In case someone is wondering about Laravel 5: just add your message to validation.php right under all the default messages. For example:
<?php
return [
"timezone" => "The :attribute must be a valid zone.",
"date_not_in_future" => "Date :attribute may not be in future.",
where date_not_in_future
is your custom function validateDateNotInFuture
.
Laravel will pick the message each time you use your rule for any field and you won't have to use custom
array unless you want to override your global message for specific fields.
Full code to implement the validator follows.
Custom Validator (with a bonus gotcha comments for date_format and date_before localization):
<?php namespace App\Services\Validation;
use Illuminate\Validation\Validator as BaseValidator;
class Validator extends BaseValidator {
public function validateDateNotInFuture($attribute, $value, $parameters)
{
$tomorrow = date('your app date format here', strtotime("tomorrow"));
$parameters[0] = $tomorrow;
return $this->validateBefore($attribute, $value, $parameters);
}
}
ValidatorServiceProvider file:
<?php namespace App\Providers;
namespace App\Providers;
use App\Services\Validation\Validator;
use Illuminate\Support\ServiceProvider;
class ValidatorServiceProvider extends ServiceProvider{
public function boot()
{
\Validator::resolver(function($translator, $data, $rules, $messages)
{
return new Validator($translator, $data, $rules, $messages);
});
}
public function register()
{
}
}
And then just add a line to config/app.php:
'App\Providers\RouteServiceProvider',
'App\Providers\ValidatorServiceProvider',