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

akaydin's avatar
Level 19

Conditional Form Not Revealing After Event Dispatch

I have this company registration form.

  • A user can have multiple companies, and a company can have multiple users. Users have roles, editor, and admin.
  • A company creation is a multi-step process.
    • The first step is to submit a company, with only a name attribute. (I will rework this later, tax ID might be added here.) Once the user enters the company's name, they submit and if it validates (required, unique, etc..) then the company is created.
  • Now the user is assigned as admin for this new company, and the company is set the current_company for this user.
  • At this point, we dispatch an event, companyCreated, which triggers the blade and reveals the secondary form only after.
  • The BusinessInfoForm reveals now.
  • HasBusinessInfo is a trait since models like Customer, Carrier, etc. can have it too.

create.blade.php

<x-app-layout>
    <x-slot name="header">
        <flux:heading size="xl">{{ __('Create Company') }}</flux:heading>
    </x-slot>

    <div>
        <div class="py-10 mx-auto max-w-7xl sm:px-6 lg:px-8">
            @livewire('create-company')

            <!-- Dynamically Rendering Additional Forms after Company Creation -->
            @if($companyCreated)
            
            <!-- Business Info Form -->
            @livewire('create-business-info', ['business_infoable' => $company])

            @endif
        </div>
    </div>
</x-app-layout>

create-company.blade.php

This is my CompanyForm.php

And this is my CreateCompany.php

What is the problem? Am I missing something? Is it about the logic?

Alpay BR

0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're facing seems to be related to the way you're handling the event dispatch and the conditional rendering of the secondary form. Let's go through the potential problems and solutions:

  1. Event Dispatching: Ensure that the event is correctly dispatched and listened to. In your CreateCompany component, you are dispatching an event named createdCompany. However, in your Blade template, you are checking for $companyCreated, which is a boolean. You need to update this boolean when the event is dispatched.

  2. Updating State: After dispatching the event, you should update the $companyCreated state to true and assign the created company to $company. This will ensure that the conditional rendering in your Blade template works as expected.

  3. Blade Template: Ensure that the Blade template is correctly checking the $companyCreated state. You should also ensure that the Livewire component is correctly passing the state to the Blade view.

Here's how you can adjust your code:

Update CreateCompany.php

Make sure to update the state after the company is created:

Ensure Blade Template is Correct

In your create.blade.php, ensure that the conditional rendering is based on the $companyCreated state:

@if($companyCreated)
    <!-- Business Info Form -->
    @livewire('create-business-info', ['business_infoable' => $company])
@endif

Verify Event Handling

Ensure that the event createdCompany is correctly handled if you have any JavaScript or Livewire listeners that need to react to it.

By ensuring that the state is updated correctly and the Blade template checks the correct state, your form should reveal as expected after the company is created.

akaydin's avatar
Level 19

It's already the same. Maybe because we are not rendering the create.blade.php

Please or to participate in this conversation.