Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rsd's avatar
Level 1

Form Validation Request for a different Model / Controller

Hello,

I need some enlightenment regarding form validation. I am still a Laravel newbie.

I have a business logic that require data to be inserted into 2 tables at the same time in a transaction. So I have:

  • AModel, BModel
  • AController, BController

When calling store on AController a BModel object needs to be created too.

So, I was happy validating the post data in the Controllers themselves. The store() on AController called a validation function in BController and everything was fine.

However, as the Laravel way, separation of concerns is important, I move the validation to:

  • ARequest and
  • BRequest

The problem arouse from the fact that I just have ARequest from

    public function store(ARequest $request, A $a): JsonResponse

I had to find a way to be able to have access to BRequest to validate the data too.

The way I found out that work is:

public function store(ARequest $request, A $a): JsonResponse
{
    $validated = $request->validated();

    // Some work on post data
    $bData = $request->all();
    ...
    // Create an instance of BRequest with the data
    $bRequest = BRequest::createFrom($request);

    // Replace the request data with the trade data
    $bRequest->replace($bData);

    // Validate BRequest
    $validatedB = app('validator')->make($bRequest->all(), $bRequest->rules())->validate();
    ...
}

Without doing this way, and just creating a FormRequest object, the FormRequest->validator will be null.

All that said, it does not seems to be the best way to do this. I believe that multi-table validation within a transaction is not something rare. So, what am I missing?

Thx,

-rsd

0 likes
2 replies
LaryAI's avatar
Level 58

To handle form validation for multiple models in a Laravel application, especially when you need to validate data for two different models within a single transaction, you can follow a more structured approach. Here's a solution that adheres to Laravel's best practices:

  1. Use Custom Form Requests: You can create custom form requests for each model. You've already done this with ARequest and BRequest.

  2. Combine Validation Logic: Instead of trying to manually invoke the validation logic of BRequest within AController, you can create a service class that handles the combined validation and transaction logic.

  3. Service Class: Create a service class that will handle the transaction and validation for both models.

Here's how you can implement this:

Step 1: Create a Service Class

Create a service class that will encapsulate the logic for handling the transaction and validation.

Step 2: Use the Service in Your Controller

In your AController, use the service class to handle the request.

Explanation

  • Service Class: The ABService class handles the transaction and validation logic. It uses Laravel's DB::transaction to ensure that both model operations are atomic.
  • Validation: The service class uses Laravel's Validator facade to manually validate the data for BRequest.
  • Separation of Concerns: This approach keeps your controller clean and delegates the business logic to a dedicated service class.

This solution provides a clean and maintainable way to handle complex validation and transaction logic in Laravel.

Snapey's avatar

One request goes to one controller.

That controller might not have anything to do with one specific model. It is controlling the request.

There you can validate all form data and update one, many or none models.

Please or to participate in this conversation.