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

Davina's avatar

Session is missing expected key [errors]

I'm getting this error while writing tests for my validation: Session is missing expected key [errors].

Here's my test code:

public function test_edit_other_settings_validation() {
    $user = User::factory()->create();
    $this->actingAs($user)
        ->post('/settings/other-settings/edit', [
            'otherSettings' => []
        ])
        ->assertSessionHasErrors([
            'otherSettings.*.key',
            'otherSettings.*.value'
        ]);
}

Controller:

public function otherSettingsUpdate(Request $request) {
    $request->validate([
        'otherSettings.*.key' => 'required|string',
        'otherSettings.*.value' => 'required|string'
    ]);
}

Routes:

Route::middleware(['auth'])->group(function () {
    Route::prefix('settings')->group(function () {
        Route::get('/other-settings/edit', [SettingsController::class, 'otherSettingsEdit'])->name('settings.other-settings.edit');
        Route::post('/other-settings/edit', [SettingsController::class, 'otherSettingsUpdate'])->name('settings.other-settings.update');
    });
});
0 likes
1 reply
SilenceBringer's avatar

@davina you test and validation works as expected. You checks every array item in otherSettings, but you do not checks that this array contains something.

If this array should contains at least 1 item - extend your validation

    $request->validate([
		'otherSettings' => 'required|array',
        'otherSettings.*.key' => 'required|string',
        'otherSettings.*.value' => 'required|string'
    ]);

or just pass empty array in request

        ->post('/settings/other-settings/edit', [
            'otherSettings' => [
				[] // this one is array item and will be validated by using otherSettings.*
			]
        ])

And the last one - session will contains error with actual indexes instead of *, so, check assertions should looks like

        ->assertSessionHasErrors([
            'otherSettings.0.key',
            'otherSettings.0.value'
        ]);
1 like

Please or to participate in this conversation.