【问题标题】:Inquiry about Validation Laravel关于验证 Laravel 的查询
【发布时间】:2018-08-07 21:24:01
【问题描述】:

我有一个关于 Laravel 验证的问题。我有两个数字 <input> 字段,例如:

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

我应该如何编码? Laravel 验证是否支持这一点?

【问题讨论】:

    标签: php forms laravel validation


    【解决方案1】:

    您可以通过运行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

    【讨论】:

    • 我明白了。非常感谢您的回答:)
    • 我也有一个问题。我使用 Request 中的 'dn_code' => 'unique:user,dn_code',但 'dn_code' 不是我输入的字段。我使用其他人的一些句子和语法从 App\http\Controller.php 创建它。如果我们不输入(不是 $request),我们应该如何检查错误“dn_code”。谢谢
    • 您可以编写自定义验证代码来处理这里的检查laravel.com/docs/5.6/validation#custom-validation-rules,您可以在其中验证该字段
    猜你喜欢
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    相关资源
    最近更新 更多