5

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 1 click on payment terms Step 1

Step 2 slect terms and confirm Step 2

Step 3 add values in payment percentage input fields those are populated from JQuery. Step 3

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

  1. Laravel Validation MessageBag contains errors however not showing up in blade file
  2. Laravel 5 validate error message not showing
  3. Laravel 8 - Old inputs and validation error messages are not working
  4. https://github.com/laravel/framework/issues/26367
  5. https://laravel.io/forum/why-laravel-validation-dont-displaying-errors
  6. https://laracasts.com/discuss/channels/laravel/error-messages-are-not-shown-after-validation
  7. 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;
}
msayubi76
  • 1,446
  • 1
  • 12
  • 18
  • 1
    The problem seem to be in the client side but you have not provided those codes. Also I too agree with @Martin’s answer. – aimme Nov 24 '21 at 21:28
  • I am using `{{$errors}}` to print errors and `print_r(old())` at blade files but both are empty. it mean there is some issue at backend side where laravel is mainting all these things behind the scene. What would you suggest? – msayubi76 Nov 25 '21 at 06:45
  • It happens only when any dynamic html comes in my form and submit it then validation applies and redirect back to form and then `$errors` and `old()` are empty. It looks realy strange for me and realy don't never ever face this strange issue before – msayubi76 Nov 25 '21 at 06:47
  • 1
    can you share the jquery populated HTML? Do the fields have a unique name property set? Does this HTML populated inside the `
    `?
    – iamab.in Nov 27 '21 at 19:57
  • What is the jQuery code? How is it adding additional elements and how are they named? – miken32 Nov 30 '21 at 19:33
  • Do you use AbcRequest in your controller `store` method? Like: `public function store(AbcRequest $request){` – Enver Arslan Nov 30 '21 at 20:18
  • yes of course @EnverArslan – msayubi76 Dec 01 '21 at 06:21
  • 1
    3 separate users have asked to see the client-side code but you keep ignoring them. – miken32 Dec 02 '21 at 16:58
  • I think this issue relating with checkbox validation. https://5balloons.info/working-with-checkbox-input-in-laravel-form/ – Enver Arslan Dec 03 '21 at 15:57

1 Answers1

0

Disclaimer: I'm sorry if I did not understand your question correctly, trying my best.

If you create some of your HTML inputs with jQuery on the client side, Laravel will not be able to insert any old input values or validation errors into your HTML since the DOM elements you're referring to simply do not exist at the time of rendering on your webserver.

Essentially, I can think of two ways to solve this: let Laravel render those HTML elements and hide them using CSS. Your JavaScript may then (conditionally?) show the previously hidden elements by manipulating CSS. Another option would be to template old input values and validation errors into JavaScript variables within an inline script tag that your jQuery code can parse, for example by encoding and decoding those values in JSON.

Martin
  • 534
  • 4
  • 14
  • Thanks @Martin for the comment. Yes but problem is that laravel doesn't send any error message and old() input values at blade files. I'm printing errors messages and old values by `{{$errors}}` and `print_r(old())` but both are empty at blade file. – msayubi76 Nov 25 '21 at 06:41
  • @Marting I've attached some images for eloboration please have a look on them. – msayubi76 Nov 25 '21 at 06:41