Skip to content

Instantly share code, notes, and snippets.

@alibo
Last active August 29, 2015 14:22
Show Gist options
  • Save alibo/8f37350839b5a05fdd0c to your computer and use it in GitHub Desktop.
Save alibo/8f37350839b5a05fdd0c to your computer and use it in GitHub Desktop.
'not_exists' rule for validator in laravel 5.0+ [Simple Solution!]
<?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