2017-01-30 5 views
0

複数の入力を持つ1つのフォームがあります。各セクションは複数の入力を持つことができ、私は彼らのためにRequests内部Form Validatorを作成したいのですが、どのように行うのか分からない...これは私がそれをやっているか、現在ある:私が持っている複数の配列入力用のカスタムリクエストバリデータ

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(); 
} 

お知らせユーザーがwork experienceまたはeducationを1つ以上選択した場合は、foreachの内部でを移動してください。これをどのように達成できますか?

私は無制限のセクションを持つことができるので、foreachを使用しています。理由は画像を参照してください。あなたがexapleのために、バリデータ自体に配列を渡すことができlaravel 5.4ので

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

答えて

0

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

は、今では

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

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

検証配列のように検証することができます 入力フォームldsは痛みである必要はありません。例えば、与えられた配列入力フィールド内の各電子メールが一意であることを検証するために、次の行うことができます:あなたの言語ファイルであなたの検証メッセージを指定するときは、*文字を使用することができ、同様に

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

、配列ベースのフィールドに対して1つの検証メッセージを使用するのが簡単です:

'custom' => [ 
    'person.*.email' => [ 
     'unique' => 'Each person must have a unique e-mail address', 
    ] 
], 
関連する問題