duanmao1975 2017-03-20 18:51
浏览 212
已采纳

如何修改此Laravel验证规则以检查表中是否存在具有此值的记录?

I am pretty new in PHP and moreover in Laravel. I am working on a Laravel 5.4 project and I have the following problem related to validation.

In the past I created this validation rules (related to a new user registration form):

$rules = array(
    'name' => 'required',
    'surname' => 'required',
    'login' => 'required|unique:pm_user,login',
    'email' => 'required|email|confirmed|unique:pm_user,email',
    'pass' => 'required|required|min:6',
    'g-recaptcha-response' => 'required|captcha',
);

In particular this rules array contains this rule:

'login' => 'required|unique:pm_user,login',

it seems to me that this last rule check if the inserted login doesn't yet exist into the pm_user table (so it ensure that not exist a row of the pm_user table having the same inserted value into the login column).

Is it? Correct me if I am doing wrong assertion.

If it work in this way now my problem is how to do the opposite thing in another set of validation rule.

In particular I have this other array of rule (defined into a class extending FormRequest:

public function rules() {
    return [
        'email' => 'required|email',
        'token' => 'required',
    ];
}

In particular I have to ensure that into the pm_user table yet exist a record having the value of the column named email that is the same of the emai field of the request.

How can I change this request to perform this validation rule?

  • 写回答

1条回答 默认 最新

  • dongtiannai0654 2017-03-20 18:57
    关注

    Laravel 5.4 already has a built in validation rule for this called exists. https://laravel.com/docs/5.4/validation#rule-exists

    I think you are looking for:

    'email' => 'required|email|exists:pm_user,email'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面