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

Saturn93's avatar

After version upgrade assertHasFormErrors no longer working

I was using Filament 3.0 previously and i have a form displaying a customer. When the customer_type is set to person, firstname and lastname are required and company is not required. If customer_type is set to company, then company is required and fistname and lastname are not.

                                Forms\Components\TextInput::make('firstname')
                                    ->requiredIf('company_type', 'person')
                                    ->maxLength(255),
                                Forms\Components\TextInput::make('lastname')
                                    ->requiredIf('company_type', 'person')
                                    ->maxLength(255),
                                Forms\Components\TextInput::make('company')
                                    ->requiredIf('company_type', 'company')
                                    ->maxLength(255),

In the Unit test i was asserting that those fields would fail with "requiredIf".

        // If company, company is required
        Livewire::test(CustomerResource\Pages\EditCustomer::class, [
            'record' => $customer->getRouteKey(),
        ])
            ->fillForm([
                'company_type' => 'company',
                'company' => null,
                'firstname' => null,
                'lastname' => null,
            ])
            ->call('save')
            ->assertHasFormErrors([
                'company' => 'requiredIf',
            ]);

        // If person, firstname and lastname is required
        Livewire::test(CustomerResource\Pages\EditCustomer::class, [
            'record' => $customer->getRouteKey(),
        ])
            ->fillForm([
                'company_type' => 'person',
                'company' => null,
                'firstname' => null,
                'lastname' => null,
            ])
            ->call('save')
            ->assertHasFormErrors([
                'firstname' => 'requiredIf',
                'lastname' => 'requiredIf',
            ]);

This worked fine, until i did a composer update. Now i have Filament 3.1 and the test fails with

Component has no matching failed rule or error message [requiredIf] for [data.company] attribute.
Failed asserting that an array contains 'requiredIf'.

Just using "required" instead of "requiredIf" also fails. Does anybody know what has changed and how i can fix this? Thanks

0 likes
1 reply
LaryAI's avatar
Level 58

It seems like the error message or validation rule key that Filament uses for requiredIf validation failures might have changed in the newer version. The assertHasFormErrors method expects the exact error message or rule that is used when the validation fails.

Here's what you can do to fix this:

  1. Check the release notes or upgrade guide for Filament 3.1 to see if there are any breaking changes related to validation or error messages.
  2. Instead of asserting the specific rule requiredIf, you can assert the presence of any error on the fields. This way, you are not dependent on the specific error message or rule key.

Here's how you can modify your tests:

// If company, company is required
Livewire::test(CustomerResource\Pages\EditCustomer::class, [
    'record' => $customer->getRouteKey(),
])
    ->fillForm([
        'company_type' => 'company',
        'company' => null,
        'firstname' => null,
        'lastname' => null,
    ])
    ->call('save')
    ->assertHasErrors([
        'company' => 'required',
    ]);

// If person, firstname and lastname are required
Livewire::test(CustomerResource\Pages\EditCustomer::class, [
    'record' => $customer->getRouteKey(),
])
    ->fillForm([
        'company_type' => 'person',
        'company' => null,
        'firstname' => null,
        'lastname' => null,
    ])
    ->call('save')
    ->assertHasErrors([
        'firstname' => 'required',
        'lastname' => 'required',
    ]);

In the above code, I've changed assertHasFormErrors to assertHasErrors and used the general 'required' rule instead of 'requiredIf'. This should work regardless of the specific error message or rule key used by Filament.

Remember to check the actual validation rules applied in your Livewire component to ensure that 'required' is indeed the rule that is failing. If Filament uses a different rule or message key, you should use that instead.

Please or to participate in this conversation.