I try to use Illuminate\Validation as standalone library and it fails with unique rule. the error is: Uncaught RuntimeException: Presence verifier has not been set. here is my code:
$fileLoader = new Translation\FileLoader(new Filesystem\Filesystem(), '');
$factory = new Validation\Factory( new Translation\Translator($fileLoader, 'en_US') );
$d = ['name' => 'te' ];
$rules = [
'name' => [Rule::unique('page')],
];
$validator = $factory->make($d, $rules);
if($validator->fails()){
$errors = $validator->errors();
foreach($errors->all() as $message){
d($message);
}
}
The unique
rule is meant to check the value to be unique in the database. The way the validator checks for "uniqueness" in the database is using a 'Presence verifier'. You didn't provide any Presence verifier and that's why you see the error. To provide a presence verifier you need to add this extra code
$presenceVerifier = new MyPresenceVerifier();
$factory->setPresenceVerifier($presenceVerifier);
where MyPresenceVerifier
is an implementation of the \Illuminate\Validation\PresenceVerifierInterface
contract. Since you are using the validator as a standalone library you will need to create your own implementation of the presence verifier for whatever database you are using. You can can find an example of implementation in the official Laravel repo:
Try this ::
$this->validate($request,
['email' => 'required|exists:users,email'],
['email.exists' => 'You are not registered User. Please register.']
);
users = Your table name
email = Your table field name