Last active
August 29, 2015 14:22
-
-
Save alibo/8f37350839b5a05fdd0c to your computer and use it in GitHub Desktop.
'not_exists' rule for validator in laravel 5.0+ [Simple Solution!]
This file contains 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 namespace App\Providers; | |
use Illuminate\Validation\Factory as Validator; | |
use Illuminate\Support\ServiceProvider; | |
class ValidatorServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap the application services. | |
* | |
* @param Validator $validator | |
* @return void | |
*/ | |
public function boot(Validator $validator) | |
{ | |
$validator->extend('not_exists', function ($attribute, $value, $parameters) use ($validator) { | |
//this is opposite of using `exists` rule, so we use exists | |
//but when it fails, it returns true! [simple solution!] | |
$v = $validator->make( | |
[ | |
$attribute => $value | |
], | |
[ | |
$attribute => "exists:" . implode(",", $parameters) | |
] | |
); | |
return $v->fails(); | |
}); | |
} | |
/** | |
* Register the application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment