【问题标题】:Laravel Validation Rule 'same' Negation CheckLaravel 验证规则“相同”否定检查
【发布时间】:2014-05-20 12:38:57
【问题描述】:

我正在尝试在 laravel 框架中编写一个简单的验证规则。

$rules = array(
    'from_account' => 'required',
    'to_account'   => 'required|same:from_account',
    'amount'       => 'required|numeric',
    'description'  => 'required'
);

现在您可以看到验证规则 same:from_account 将检查并且必须要求 to_accountfrom_account 完全相同 我正在寻找完全相反的验证,因此 to_account 不能相同作为from_account

有什么方法可以在规则中告诉这个否定检查还是我必须手动检查它?

【问题讨论】:

    标签: php validation laravel laravel-4


    【解决方案1】:

    为什么不查看文档here

    然后使用这个:

    'to_account'   => 'required|different:from_account',
    

    甚至(为了有趣):

    'from_account' => 'required|different:to_account',
    'to_account'   => 'required|different:from_account',
    

    【讨论】:

    • 啊,谢谢。我错过了列表中的这条规则。谢谢。
    • @Rana 当然,一直在发生。 :)
    • @majidarif,干得好。这需要多个比较字段还是只需要一个?
    【解决方案2】:

    使用different 规则:

    $rules = array(
        'from_account'  => 'required',
        'to_account'    => 'required|different:from_account',
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2018-02-18
      • 2021-05-12
      相关资源
      最近更新 更多