doupinwan0563
2016-11-28 10:01
浏览 39
已采纳

如何验证电子邮件是否已经使用流明?

I am new to Lumen (Laravel). I want to validate if email is already taken by following the Lumen documentation .

$this->validate($request, [
        'email' => 'required|unique:borrower|max:255',
        //'mobile' => 'required|unique:borrower|max:255'

    ]);

If I execute the following line in postman, it shows like 422 unprocessed entity.

How can I check if email is already taken through Lumen and show the error message in view? Thanks for all the help!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doumaoao0182 2016-11-28 10:11
    已采纳

    For Inserting new entry

    function validateUser($request){
        $validator = Validator::make($request->all(), [
            'email' => 'bail|required|email|unique:borrower,email',
        ]);
        return $validator;
    }
    

    For Updating

    function validateUser($request){
        $validator = Validator::make($request->all(), [
            'email' => 'bail|required|email|unique:borrower,email,'.$request->id.',id',
        ]);
        return $validator;
    }
    

    Combining both

    function validateUser($request){
        $id = isset($request->id) ? ','.$request->id.',id':'';
        $validator = Validator::make($request->all(), [
            'first_name' => 'required',
            'email' => 'bail|required|email|unique:borrower,email'.$id,
        ]);
        return $validator;
    }
    

    After that you can pass $validator to display all error.

    Bail : bail checks first condition, if fails it will not check for email valiation or unique checks.

    点赞 评论
  • dqd78456 2016-11-28 10:08

    Is borrower your table name that email should be unique in?

    According to the docs:

    unique:table,column,except,idColumn

    The field under validation must be unique on a given database table. If the column option is not specified, the field name will be used.

    Specifying A Custom Column Name:

    'email' => 'unique:users,email_address'
    

    https://laravel.com/docs/5.2/validation#rule-unique

    点赞 评论

相关推荐 更多相似问题