How to handle multiple form Request and their validation from a single page with same controller.
what i'm trying to do is, submit any form on the page separately, the problem lies in validation, when i submit form2, the validate($request) for form1 sends back the error, i want to separate the request received from the different forms and then handle them accordingly, (i just want to validate the form user is submitting at the moment and not care about the other forms at that particular moment)
My forms are like this:
-
Image Form:
@if(count($errors) > 0)-
@foreach($errors->all() as $error)
- {{ $error }} @endforeach
@if(session('error')) <div class="alert alert-danger"> <ul> <li>{{ session('error') }}</li> </ul> </div> @endif {{ csrf_field() }} <input type="file" name="coverImage" class="form-control" required> <input type="submit" name="cover_upload" value="Upload" class="btn btn-success pull-right"> -
profile form
@if(count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif @if(session('error')) <div class="alert alert-danger"> <ul> <li>{{ session('error') }}</li> </ul> </div> @endif {{ csrf_field() }}
<input type="text" name="name" class="form-control" placeholder="Name of Institute" required>
<input type="text" name="campus" class="form-control" placeholder="Campus Name here" required>
<input type="text" name="contact" class="form-control" placeholder="contact detail here (email or phone #)" required>
<input type="text" name="address" class="form-control" placeholder="Address of the Institute" required>
<input type="submit" name="profile_upload" value="Upload Profile" class="btn btn-success pull-right">
</form>
my Controller is like this:
public function postUploadImage(Request $request){
$this->validate($request, [
'coverImage' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:1024',
]);
$file = $request->file('coverImage');
$ext = $file->guessClientExtension();
$file->storeAs('public/institutes/covers/'. Sentinel::getUser()->id. '/', "cover.{$ext}");
return back()->with('success','Image upload Successful !');
}
public function postProfile(Request $request){
dd($request->all());
// other code here
}
my Routes are like this: Route::get('/upload', 'InstituteController@getProfile');
Route::post('/upload', 'InstituteController@postProfile');
Route::post('/upload', 'InstituteController@postUploadImage');
Now when i submit from that is handled by postProfile function, the other function postUploadImage also checks the validation for other form which only uploads the profile image.
what i want to do is to just Execute the function which is associated with the form that has just submitted.
suggestion and recommendations are really appreciated. thanks.
Please or to participate in this conversation.