Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在数组名Laravel中验证_Laravel_Laravel 5.4 - Fatal编程技术网

在数组名Laravel中验证

在数组名Laravel中验证,laravel,laravel-5.4,Laravel,Laravel 5.4,如何通过Id laravel进行验证 现在我在控制器中使用它。我的问题是在我的html中,我的输入类型文本如下 <input type="text" name="quantity_box[]" class="form-control" autofocus="" /> <input type="text" name="quantity_box[1]" class="form-control" autofocus="" /> <input type="text" nam

如何通过Id laravel进行验证

现在我在控制器中使用它。我的问题是在我的html中,我的输入类型文本如下

<input type="text" name="quantity_box[]" class="form-control" autofocus="" />
<input type="text" name="quantity_box[1]" class="form-control" autofocus="" />
<input type="text" name="quantity_box[2]" class="form-control" autofocus="" />

既然您正在验证$request变量,那么就应该验证输入名称

如果您使用的是Laravel5.2+,那么可以像这样验证数组

 $validator = Validator::make($request->all(), [
        'quantity_box.*' => 'required',
     ]);

为了获得最佳实践,我建议使用Laravel的5.5表单请求验证

使用这种方法,您将尽可能保持控制器代码的干净


首先,我们请求将验证和身份验证规则存储在

php artisan make:request myQuantityBoxRequest

myquantityboxrequest.php

<?php 

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Auth;

class myQuantityBoxRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     * The user is always authorized here to make the request
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'quantity_box.*' => 'required'
        ];
    }
}

好了。如果您使用此函数,它将验证输入,就像您正在使用
$this->validate()

是否仍要更改“按id验证”?因为它可能是数量箱[10]我不知道将插入多少用户data@test1321是的,有一种方法。你能给我举个例子吗@larsmertens为了获得最佳实践,我建议使用
表单请求验证
谢谢你,先生,我会检查出来的。我已经把这个方法的一个例子作为第二个答案,以防你或其他人想要使用它谢谢你,先生这让我更了解谢谢,先生
<?php 

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Auth;

class myQuantityBoxRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     * The user is always authorized here to make the request
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'quantity_box.*' => 'required'
        ];
    }
}
use App\Http\Requests\myQuantityBoxRequest;

public function postQuantityBoxData(myQuantityBoxRequest $request){
     // Do something after validation here
}