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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度