Skip to content

A free open source IT asset/license management system

Fixed weird multi-error alert

snipe5f195a659ff3
diff --git a/resources/views/blade/notifications.blade.php b/resources/views/blade/notifications.blade.php
index aa7ce762a1..9c24d52e35 100644
--- a/resources/views/blade/notifications.blade.php
+++ b/resources/views/blade/notifications.blade.php
@@ -131,18 +131,25 @@
<x-alert type="warning" icon="warning" :title="trans('general.notification_error')" aria-live="assertive" aria-atomic="true">
<button type="button" class="close" data-dismiss="alert" aria-label="{{ trans('general.close') }}"><span aria-hidden="true">&times;</span></button>
<ul>
- @foreach (array_splice($messages, 0, 3) as $key => $message)
+ @foreach (array_slice($messages, 0, 3) as $message)
<li>{{ $message }}</li>
@endforeach
</ul>
- <details>
- <summary>{{ trans('general.show_all') }}</summary>
- <ul>
- @foreach (array_splice($messages, 3) as $key => $message)
- <li>{{ $message }}</li>
- @endforeach
- </ul>
- </details>
+ {{-- Only render the disclosure when there's actually
+ something behind it. Previous shape used array_splice
+ (destructive) and rendered <details><summary>Show all</summary>
+ unconditionally, so a 3-or-fewer message set produced
+ an empty "Show all" toggle. --}}
+ @if (count($messages) > 3)
+ <details>
+ <summary>{{ trans('general.show_all') }}</summary>
+ <ul>
+ @foreach (array_slice($messages, 3) as $message)
+ <li>{{ $message }}</li>
+ @endforeach
+ </ul>
+ </details>
+ @endif
</x-alert>
</div>
@endif
diff --git a/tests/Feature/Checkins/Ui/AssetCheckinInvalidModelTest.php b/tests/Feature/Checkins/Ui/AssetCheckinInvalidModelTest.php
new file mode 100644
index 0000000000..9188292f37
--- /dev/null
+++ b/tests/Feature/Checkins/Ui/AssetCheckinInvalidModelTest.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace Tests\Feature\Checkins\Ui;
+
+use App\Models\Asset;
+use App\Models\User;
+use Illuminate\Support\Facades\DB;
+use Tests\TestCase;
+
+/**
+ * Regression for the "GET /hardware/{id}/checkin bounces to
+ * hardware.edit with a generic 'check the form for errors' but
+ * nothing visibly highlighted" report. AssetCheckinController::create
+ * runs $asset->isInvalid() before rendering the checkin form. If a
+ * model-level validation rule fails on a field the user can't (or
+ * doesn't) see, they used to get only the generic top-of-page alert
+ * with no indication of which field failed.
+ *
+ * The fix flashes the specific validation messages via the shared
+ * multi_error_messages session key so notifications.blade.php
+ * surfaces them as a bullet list separate from the generic alert.
+ * Inline .has-error styling on visible fields still fires from
+ * withErrors($errors); those aren't duplicated in the multi_error
+ * alert because they're already covered inline. The multi_error
+ * alert is essential for the off-form / off-screen case.
+ */
+class AssetCheckinInvalidModelTest extends TestCase
+{
+ public function test_checkin_get_flashes_specific_errors_when_asset_fails_model_validation(): void
+ {
+ $asset = Asset::factory()->assignedToUser()->create();
+
+ // Bypass model events + validation to write an invalid value
+ // directly. warranty_months has a `digits_between:0,240` rule
+ // (Asset::$rules), so 999 will trip isInvalid() at checkin time.
+ // Using DB::update rather than $asset->forceSave() keeps us
+ // out of the observer stack entirely.
+ DB::table('assets')->where('id', $asset->id)->update(['warranty_months' => 999]);
+
+ $response = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('hardware.checkin.create', $asset));
+
+ $response->assertStatus(302);
+ $response->assertRedirect(route('hardware.edit', $asset));
+
+ // The generic errors bag is still populated so inline
+ // .has-error styling fires on any visible fields.
+ $response->assertSessionHasErrors('warranty_months');
+
+ // AND the specific message is flashed so notifications.blade.php
+ // renders it in the top alert, catching the off-form case.
+ $response->assertSessionHas('multi_error_messages', function ($messages) {
+ return is_array($messages)
+ && count($messages) > 0
+ && collect($messages)->contains(fn ($msg) => str_contains(strtolower($msg), 'warranty'));
+ });
+ }
+
+ public function test_multi_error_messages_alert_hides_show_more_when_three_or_fewer(): void
+ {
+ // Companion regression: notifications.blade.php used to render
+ // an empty <details><summary>Show all</summary> block for any
+ // multi_error_messages payload, even when there were 3 or
+ // fewer items and nothing to disclose.
+ session()->flash('multi_error_messages', ['one', 'two', 'three']);
+
+ $response = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('hardware.index'));
+
+ $content = $response->getContent();
+ $this->assertStringContainsString('<li>one</li>', $content);
+ $this->assertStringContainsString('<li>three</li>', $content);
+ $this->assertStringNotContainsString('<summary>', $content, 'Show-more disclosure must not render for 3 or fewer messages.');
+ }
+
+ public function test_multi_error_messages_alert_shows_show_more_when_more_than_three(): void
+ {
+ session()->flash('multi_error_messages', ['one', 'two', 'three', 'four']);
+
+ $response = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('hardware.index'));
+
+ $content = $response->getContent();
+ $this->assertStringContainsString('<summary>', $content);
+ $this->assertStringContainsString('<li>four</li>', $content);
+ }
+}