Last active
March 7, 2017 23:35
-
-
Save paulofreitas/9b9137136e7bc5911cac6e86e3be1bcc to your computer and use it in GitHub Desktop.
Using custom Validator in Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // resources/lang/en/validation.php | |
return [ | |
// ... | |
'foo' => 'The :attribute must be a valid "foo".', | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // resources/lang/pt/validation.php | |
return [ | |
// ... | |
'foo' => 'O :attribute deve ser um "foo" válido.', | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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