多个阵列输入的自定义请求验证器

时间:2017-01-30 01:15:53

标签: laravel laravel-5.3

我有1个表单,有多个输入。每个部分都可以有多个输入,我想为他们在Form Validator内创建一个Requests,但不知道该怎么做......这就是我现在这样做的方式:< / p>

public function postCreateResume(Request $request, Resume $resume, Education $education)
{

    /* 
     * begin a transaction, because we 
     * are doing multiple queries
     */
    DB::beginTransaction();

    /*
     * first we must create the resume, then  we 
     * can use the id for the following rows
     */
    $this->validate($education, [
        'resume_title' => 'required',
        'expected_level' => 'required',
        'salary' => 'required',
        'work_location' => 'required',
        'year_experience' => 'required',
        'about' => 'required',
    ]);     

    $resume->name = $request['resume_title'];
    $resume->work_level = $request['expected_level'];
    $resume->salary = $request['expected_salary'];
    $resume->country = $request['work_location'];
    $resume->total_experience = $request['year_experience'];
    $resume->about = $request['about'];
    $resume->save();

    // a user can have multiple educations on their cv
    foreach($request->input('education') as $education){

        $this->validate($education, [
            'institution' => 'required',
            'degree' => 'required',
            'year_begin' => 'required',
            'year_finish' => 'required',
            'about' => 'required',
        ]);

        // passed our checks, insert
        $education->resume_id = $resume->id;
        $education->user_id = Auth::user()->id;
        $education->institute = $education['institution'];
        $education->degree = $education['degree'];
        $education->summary = $education['about'];
        $education->started = $education['year_begin'];
        $education->ended = $education['year_finish'];

        if(!$education->save()){
            DB::rollback();
            return redirect()->back()->withErrors("There was an error creating this resume")->withInput();
        }
    }

    // a user can have multiple employment on their cv
    foreach($request->input('experience') as $employment){

        $this->validate($employment, [
            'company' => 'required',
            'title' => 'required',
            'country' => 'required',
            'year_begin' => 'required',
            'year_finish' => 'required',
            'notes' => 'required',
        ]);

        // passed our checks, insert
        $employment->resume_id = $resume->id;
        $employment->user_id = Auth::user()->id;
        $employment->name = $employment['title'];
        $employment->company = $employment['company'];
        $employment->country = $employment['country'];
        $employment->started = $employment['year_begin'];
        $employment->ended = $employment['year_finish'];
        $employment->summary = $employment['notes'];

        if(!$employment->save()){
            DB::rollback();
            return redirect()->back()->withErrors("There was an error creating this resume")->withInput();
        }
    }

    return redirect()->back()->withSuccess("You have created a resume")->withInput();
}

注意我在每个foreach内都有验证,以防用户选择了多于1个(在此示例中)work experienceeducation,我正在尝试做什么将$this->validate移到Requests文件夹中,我该如何实现?

我正在使用foreach,因为我可以拥有无​​限的部分,请参阅图片了解原因;

https://i.gyazo.com/74c7657e7b561f823a8b3960bc056511.png

1 个答案:

答案 0 :(得分:0)

从laravel 5.4开始,你可以将数组传递给验证器本身,例如

<input name="myarray[0]['test'] type="text">

现在可以像这样验证

$this->validate($request, [
    'myarray.*.test' => 'required'
]);

https://laravel.com/docs/5.4/validation#validating-arrays

验证基于数组的表单输入字段并不是一件痛苦的事。例如,要验证给定数组输入字段中的每封电子邮件是否唯一,您可以执行以下操作:

$validator = Validator::make($request->all(), [
    'person.*.email' => 'email|unique:users',
    'person.*.first_name' => 'required_with:person.*.last_name',
]);

同样,您可以在语言文件中指定验证消息时使用*字符,使基于数组的字段使用单个验证消息变得轻而易举:

'custom' => [
    'person.*.email' => [
        'unique' => 'Each person must have a unique e-mail address',
    ]
],