2017-01-30 6 views
0

Ich habe 1 Formular, mit mehreren Eingängen. wobei jeder Abschnitt mehrere Eingänge haben, kann ich einen Form Validator innerhalb Requests für sie erstellen möchten, aber nicht wissen, wie es zu tun ... Das ist zur Zeit, wie ich es tue:Benutzerdefinierte Anfrage Validatoren für mehrere Array-Eingänge

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

Hinweis Ich habe die Überprüfen Sie in jedem der foreach für den Fall, dass der Benutzer mehr als 1 (in diesem Beispiel) work experience, oder education gewählt hat, was ich versuche, verschieben Sie die $this->validate in den Ordner Anfragen, wie kann ich das erreichen?

Ich verwende eine foreach, weil ich unbegrenzte Abschnitte haben kann, siehe das Bild, warum;

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

Antwort

0

Seit Laravel 5.4 Sie Arrays mit dem Validator selbst für exaple

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

wie so validiert werden

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

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

Validieren Array übergeben werden kann, kann jetzt basierte Form Eingabe fie lds muss kein Schmerz sein. Zum Beispiel, zu validieren, dass jede E-Mail in einem Array Eingang bestimmten Bereich einzigartig ist, können Sie Folgendes tun:

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

Ebenso können Sie die Zeichen * verwenden, wenn die Überprüfungsmeldungen in Ihrer Sprache Dateien an, es ist ein Kinderspiel, eine einzige Validierungsnachricht für array-basierte Felder zu verwenden:

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