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

前端 未结 8 2222
情歌与酒
情歌与酒 2020-11-27 15:08

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:          


        
8条回答
  •  囚心锁ツ
    2020-11-27 15:52

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

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

    so as stated by @Sarpadoruk as of laravel 5.6 laravel added features in validation like gt,gte,lt and lte which means:

    • gt - greater than
    • gte - greater than equal to
    • lt - less than
    • lte - less than equal to

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

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

提交回复
热议问题