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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。