mihirpatel83's avatar

Dependent Dropdown After Validation

Hello, I have a parent dropdown and when parent is selected, I populate 3 child dropdown using ajax. Now those child dropdown are also required fields. But before i select them and i validate the form, the child dropdown are populating blank. So how can i get the old value of parent dropdown in Controller action so that I can populate those dependent child dropdowns after validation.

I tried to search forum but could not find the solution. If there's a better approach to validate and populate dropdowns then would be of great help.

0 likes
5 replies
manojo123's avatar

Try to validate the fields like this:

'child_dropdown' => 'required_with:parent_dropdown'
mihirpatel83's avatar

@MANOJOW - I am yet not validating the child dropdowns. My validation rules is just validating the parent dropdown along with other fields. So basically the steps are as below:

  1. Selecting a value from parent dropdown
  2. Display three child dependent dropdown
  3. Click submit fires 'Required for another form field' and this makes the child dropdown empty after the form loads.

so when validation occurs, I will need to fetch the parent dropdown value in Create action and based on it will have to prepopulate child dropdowns but I was wondering how to get old values in Create action?

manojo123's avatar

As I understand, you need to get the old value of parentDropdown in Controller.

Please read this documentation. https://laravel.com/docs/5.7/requests#old-input

I will write down the significant part to your problem and edit the samples to make it easier for you to understand.

Laravel allows you to keep input from one request during the next request. This feature is particularly useful for re-populating forms after detecting validation errors

Flashing Input To The Session The flash method on the Illuminate\Http\Request class will flash the current input to the session so that it is available during the user's next request to the application:

$request->flash();

You may also use the flashOnly and flashExcept methods to flash a subset of the request data to the session. These methods are useful for keeping sensitive information such as passwords out of the session:

$request->flashOnly(['parentDropdown']);

$request->flashExcept('password');

Flashing Input Then Redirecting Since you often will want to flash input to the session and then redirect to the previous page, you may easily chain input flashing onto a redirect using the withInput method:

return redirect('form')->withInput();

return redirect('form')->withInput(
    $request->except('password')
);

Retrieving Old Input To retrieve flashed input from the previous request, use the old method on the Request instance. The old method will pull the previously flashed input data from the session:

$parentDropdown = $request->old('parentDropdown');

Laravel also provides a global old helper. If you are displaying old input within a Blade template, it is more convenient to use the old helper. If no old input exists for the given field, null will be returned:

<input type="text" name="parentDropdown" value="{{ old('parentDropdown') }}">
mihirpatel83's avatar
$this->validate($request, [
            'article_id' => 'required_',
            'occasion_id' => 'required',
        ]);
 Product::create($request->all())
 return redirect()->route('products.index')->with('success', 'Product added successfully.');

to

$validate = $this->validate($request, [
            'article_id' => 'required_',
            'occasion_id' => 'required',
]);
if ($validate->fails()) {
        return redirect()->route('products.create')->withInput();
} else {
        Product::create($request->all());
        return redirect()->route('products.index')->with('success', 'Product added successfully.');
}

Now I can access the $request->old() values in Create action so as to prepopulate dropdowns. Thank you for your detailed explanation.

Please or to participate in this conversation.