如何在laravel 3的验证中查询另外两个输入? [英] How to query two more input in laravel 3's validation?

查看:56
本文介绍了如何在laravel 3的验证中查询另外两个输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,用户将userNameemail传递给我,我想进行自定义验证,以检查数据库的user表中是否有同时包含userName是否等于状态1?定制验证如何实现?

For example, the user pass the userName, email to me, and I would like to have a custom validation for check the DB's user table have a column with the both userName equal and email equal and status equal 1 or not? How the customised validation implements?

例如:

用户输入:

用户名:彼得

userName: Peter

电子邮件:peter@email.com

email: peter@email.com

在情况1中,在数据库的用户表中:

In case 1, in the DB's user table:

Success: userName: Peter , email: peter@email.com, status: 1
Fail: userName: Peter , email: peter@email.com, status: 0
Fail: userName: Mary , email: peter@email.com, status: 1
Fail: userName: Peter , email: mary@email.com, status: 1

推荐答案

您可以创建一个自定义验证方法作为全部捕获方法.这里的主要问题是验证扩展将仅将单个属性传递给方法,而不是将所有三个属性的值传递给方法.这将需要您修改验证.由于表,列名和输入的硬编码性质,该方法将非常适合您的特定应用程序.它还没有提供任何方法来说明问题所在的领域,并且还需要一些其他规则.另一个建议是实际上将验证器类扩展为一个库,以为您提供针对这种情况的更优化的验证引擎.

You can create a custom validation method as a catch all. The major problem here is that the validation extension will only ever pass the single attribute to the method rather than the values of all three. This will require you to hack up the validation. This method will be very bespoke to your particular application due to the hard coded nature of the table, column names and input. It also does not give you any way of telling which field the issue is with and would require some additional rule. Another suggestion would be to actually extend the validator class as a library to provide you with a much finer tuned validation engine for this circumstance.

Validator::register('usercheck', function($attribute, $value, $parameters)
{
    $count = DB::table('user')
      ->where('userName', '=', Input::get('userName'))
      ->where('email', '=', Input::get('email'))
      ->where('status', '=', Input::get('status'))
      ->count();

    return $count > 0;
});

要使用它,只需将其添加为一个规则即可.请记住,这有点怪癖,并且有更好的方法可以做到这一点,最引人注目的是我在开篇段落中建议的方法.

To use it just add it as a rule... bear in mind this feels a bit hacky and there ARE better ways of doing this, most notably the method I suggested in the opening paragraph.

$rules = array(
    'userName' => 'usercheck'
);

这篇关于如何在laravel 3的验证中查询另外两个输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆