I am using FormRequest to validate data. I know this question was asked many times, and the community tried to solve this issue in many ways, but none of them solved my problem. Reference links are attached.
My Problem Description
I've complex form some of its HTML is generated from my JQuery code. When Jquery code does not populate on the form, backend validations and old values work fine, but old values and validation errors don't work when JQuery HTML appears in the form and submits the form. Images are attached for more explaination.
Step 2 slect terms and confirm
Step 3 add values in payment percentage input fields those are populated from JQuery.
FormRequest
class AbcRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'payment_term' => ['required'],
'payment_percent' => ['required'],
'other_info' => ['required'],
];
}
}
web.php
Route::group(['middleware' => ['auth', 'approved']], function () {
Route::post('/abc/store', 'AbcController@store')
->name('abc.store');
});
Blade
<form method="post" action="{{ url('/abc/store') }}"
enctype="multipart/form-data">
@csrf
<input type="text" name="payment_term" class="form-control"
value="{{ old('payment_term') }}">
@error('payment_term')
<div class="backend-error"> {{ $message }} </div>
@enderror
</form>
What I've tried so far
Community Links
- Laravel Validation MessageBag contains errors however not showing up in blade file
- Laravel 5 validate error message not showing
- Laravel 8 - Old inputs and validation error messages are not working
- https://github.com/laravel/framework/issues/26367
- https://laravel.io/forum/why-laravel-validation-dont-displaying-errors
- https://laracasts.com/discuss/channels/laravel/error-messages-are-not-shown-after-validation
- Laravel 5.2 validation errors not appearing
Controller's code suggested from community
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'payment_term' => ['required'],
'payment_percent' => ['required'],
'other_info' => ['required'],
]);
if ($validator->fails()):
return redirect()->back()
->withErrors($validator)
->withInput();
endif;
}