简体   繁体   English

Laravel:验证需要大于另一个的 integer 字段

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

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_page needs to be greater than initial_page .现在, end_page需要大于initial_page How include this filter?如何包括这个过滤器?

There is no built-in validation that 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.没有内置验证可以让您比较Laravel 中的字段值,因此您需要实现自定义验证器,以便您在需要时重用验证。 Luckily, Laravel makes writing custom validator really easy .幸运的是,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 .您可以在此处找到有关创建自定义验证器的更多信息: 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.它们易于定义,然后可以在您验证数据的任何地方使用。

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 , lt and lte which means:所以正如@Sarpadoruk 所述,从 laravel 5.6 开始,laravel 在验证中添加了诸如gtgteltlte ,这意味着:

  • gt - greater than gt - 大于
  • gte - greater than equal to gte - 大于等于
  • lt - less than lt - 小于
  • lte - less than equal to lte - 小于等于

so using gt you 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'
]; 

对于 Laravel 5.4,它将是:

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

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

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'
]; 

Why not just define $min_number = $min + 1 number and use validator min:$min_number , example:为什么不定义$min_number = $min + 1 number 并使用验证器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.然后您可以返回自定义错误消息以向用户解释错误。

use gt = grater than :value|field使用 gt = 大于 :value|field

use gte = grater than equal :value|field使用 gte = 大于等于 :value|field

use lt = less than :value|field使用 lt = 小于 :value|field

use lte = less than equal :value|field使用 lte = 小于等于 :value|field

in your case it's在你的情况下

gt:initial_page

and the result will be结果将是

$rules = array(
      'initial_page' => 'required_with:end_page|numeric|min:1|digits_between: 1,5',
      'end_page' => 'required_with:initial_page|numeric|gt:initial_page|min:2|digits_between:1,5'
      );

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'
]; 

In Laravel 9.x you can use gt:field在 Laravel 9.x 中你可以使用gt:field

For example:例如:

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

https://laravel.com/docs/9.x/validation#rule-gt https://laravel.com/docs/9.x/validation#rule-gt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM