dongxiane0395 2018-08-29 08:42
浏览 106
已采纳

laravel验证独特的规则

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);
    }
}
  • 写回答

2条回答 默认 最新

  • duanping2695 2018-08-29 09:09
    关注

    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:

    https://github.com/laravel/framework/blob/master/src/Illuminate/Validation/DatabasePresenceVerifier.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b