php Laravel:验证需要大于另一个的整数字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32036882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:45:49  来源:igfitidea点击:

Laravel: validate an integer field that needs to be greater than another

phplaravellaravel-5

提问by Alexandre Thebaldi

I have two fields that are optional only if both aren't present:

我有两个字段,仅当两者都不存在时才可选:

$rules = [
  'initial_page' => 'required_with:end_page|integer|min:1|digits_between: 1,5',
  'end_page' => 'required_with:initial_page|integer|min:2|digits_between:1,5'
]; 

Now, end_pageneeds to be greater than initial_page. How include this filter?

现在,end_page需要大于initial_page。如何包括这个过滤器?

回答by jedrzej.kurylo

There is no built-in validationthat would let you compare field values like that in Laravel, so you'll need to implement a custom validator, that will let you reuse validation where needed. Luckily, Laravel makes writing custom validator really easy.

没有内置的验证,它可以让你比较字段值一样,在Laravel,所以你需要实现一个自定义的验证,这将让你重新使用验证需要的地方。幸运的是,Laravel 使编写自定义验证器变得非常容易

Start with defining new validator in yor AppServiceProvider:

首先在你的AppServiceProvider 中定义新的验证器:

class AppServiceProvider extends ServiceProvider
{
  public function boot()
  {
    Validator::extend('greater_than_field', function($attribute, $value, $parameters, $validator) {
      $min_field = $parameters[0];
      $data = $validator->getData();
      $min_value = $data[$min_field];
      return $value > $min_value;
    });   

    Validator::replacer('greater_than_field', function($message, $attribute, $rule, $parameters) {
      return str_replace(':field', $parameters[0], $message);
    });
  }
}

Now you can use your brand new validation rule in your $rules:

现在您可以在$rules 中使用全新的验证规则

$rules = [
  'initial_page' => 'required_with:end_page|integer|min:1|digits_between: 1,5',
  'end_page' => 'required_with:initial_page|integer|greater_than_field:initial_page|digits_between:1,5'
]; 

You'll find more info about creating custom validators here: http://laravel.com/docs/5.1/validation#custom-validation-rules. They are easy to define and can then be used everywhere you validate your data.

您可以在此处找到有关创建自定义验证器的更多信息:http: //laravel.com/docs/5.1/validation#custom-validation-rules。它们易于定义,然后可以在您验证数据的任何地方使用。

回答by Nagibaba

For Laravel 5.4 it will be:

对于 Laravel 5.4,它将是:

$rules = ['end_page'=>'min:'.(int)$request->initial_page]

回答by Sarpdoruk Tahmaz

As of Laravel 5.6 gt, gte, ltand lterules are added.

从 Laravel 5.6 开始gt,添加了gteltlte规则。

回答by Haritsinh Gohil

the question was asked in 2015 so most of the answers are also outdated now in 2019

这个问题是在 2015 年提出的,所以大部分答案现在在 2019 年也已经过时了

i want to give answer which uses features provided by laravel team which is included in it's new version,

我想给出的答案使用了 laravel 团队提供的功能,这些功能包含在新版本中,

so as stated by @Sarpadoruk as of laravel 5.6 laravel added features in validation like gt,gte,ltand ltewhich means:

因此通过作为@Sarpadoruk表述为laravel在验证5.6 laravel添加的功能,如 gtgteltlte该装置:

  • gt- greater than
  • gte- greater than equal to
  • lt- less than
  • lte- less than equal to
  • gt- 比...更棒
  • gte- 大于等于
  • lt- 少于
  • lte- 小于等于

so using gtyou can check that your end_page should be greater than your initial_page and your task becomes very easy now:

所以使用gt你可以检查你的 end_page 应该大于你的 initial_page 并且你的任务现在变得非常简单:

$rules = [
  'initial_page' => 'required_with:end_page|integer|min:1|digits_between: 1,5',
  'end_page' => 'required_with:initial_page|integer|gt:initial_page|digits_between:1,5'
]; 

回答by Kalhan.Toress

I think you can try something like this,

我想你可以尝试这样的事情,

$init_page = Input::get('initial_page');

$rules = [
    'initial_page' => 'required_with:end_page|integer|min:1|digits_between: 1,5',
    'end_page' => 'required_with:initial_page|integer|min:'. ($init_page+1) .'|digits_between:1,5'
]; 

回答by Mahmoud Ali Kassem

Why not just define $min_number = $min + 1number and use validator min:$min_number, example:

为什么不定义$min_number = $min + 1number 并使用 validator min:$min_number,例如:

$min = intval($data['min_number']) + 1;

return ['max_number'  => 'required|numeric|min:'.$min];

And you can then return custom error message to explain the error to user.

然后您可以返回自定义错误消息以向用户解释错误。

回答by coatesap

If you're maintaining a project on Laravel 5.2 then the following should backport the current gt rule for you:

如果你在 Laravel 5.2 上维护一个项目,那么以下应该为你反向移植当前的 gt 规则:

class AppServiceProvider extends ServiceProvider
{
  public function boot()
  {
    Validator::extend('gt', function($attribute, $value, $parameters, $validator) {
      $min_field = $parameters[0];
      $data = $validator->getData();
      $min_value = $data[$min_field];
      return $value > $min_value;
    });   

    Validator::replacer('gt', function($message, $attribute, $rule, $parameters) {
      return sprintf('%s must be greater than %s', $attribute, $parameters[0]);
    });
  }
}

You can then use it as per the current documentation:

然后您可以按照当前文档使用它:

$rules = [
  'end_page' => 'gt:initial_page'
];