首页 文章

询问验证Laravel

提问于
浏览
0

我对Laravel验证有疑问 . 我有两个数字 <input> 字段,如:
enter image description here

我想在App \ Requests \ MyRequest.php的“请求”文件中创建一个规则,该规则要求第二个输入字段的值大于第一个输入字段,并且两个字段的值必须大于0 .

我该怎么编码呢? Laravel验证是否支持此功能?

1 回答

  • 2

    您可以通过运行 php artisan make:request MyRequestapp/Http/Requests 中添加验证,如下所示:

    <?php
    
    namespace App\Http\Requests;
    
    class MyRequest extends Request
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
        */
        public function authorize()
        {
           return true;
         }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                 'first_field' => 'min:0',
                 'second_field' => 'min:'.$this->first_field,
            ];
       }
    }
    

    您可以找到有关验证的更多信息here

相关问题