Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Last active March 7, 2017 23:35
Show Gist options
  • Save paulofreitas/9b9137136e7bc5911cac6e86e3be1bcc to your computer and use it in GitHub Desktop.
Save paulofreitas/9b9137136e7bc5911cac6e86e3be1bcc to your computer and use it in GitHub Desktop.
Using custom Validator in Laravel
<?php // resources/lang/en/validation.php
return [
// ...
'foo' => 'The :attribute must be a valid "foo".',
];
<?php // resources/lang/pt/validation.php
return [
// ...
'foo' => 'O :attribute deve ser um "foo" válido.',
];
<?php // app/Providers/ValidationServiceProvider.php
namespace App\Providers;
use App\Validation\Validator;
use Illuminate\Support\ServiceProvider;
class ValidationServiceProvider extends ServiceProvider
{
public function boot()
{
$this->registerCustomValidator();
}
public function registerCustomValidator()
{
$this->app->validator->resolver(
function ($translator, $data, $rules, $messages) {
return new Validator($translator, $data, $rules, $messages);
}
);
}
}
<?php // app/Support/Validation/Validator.php
namespace App\Validation;
use Illuminate\Validation\Validator as BaseValidator;
class Validator extends BaseValidator
{
protected function validateFoo($attribute, $value)
{
// your implementation here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment