Fix issue with edit user form

Allow setting no school for a user from the admin edit user form.

Closes #59
This commit is contained in:
Matt Young 2024-08-04 15:51:36 -05:00
parent deebcc81ae
commit 95cf05f0d6
3 changed files with 28 additions and 2 deletions

View File

@ -56,11 +56,10 @@ class UserController extends Controller
'email' => ['required', 'email'], 'email' => ['required', 'email'],
'cell_phone' => ['required'], 'cell_phone' => ['required'],
'judging_preference' => ['required'], 'judging_preference' => ['required'],
'school_id' => ['required', 'exists:schools,id'], 'school_id' => ['nullable', 'exists:schools,id'],
]); ]);
$validData['is_admin'] = $request->get('is_admin') == 'on' ? 1 : 0; $validData['is_admin'] = $request->get('is_admin') == 'on' ? 1 : 0;
$validData['is_tab'] = $request->get('is_tab') == 'on' ? 1 : 0; $validData['is_tab'] = $request->get('is_tab') == 'on' ? 1 : 0;
$user->update([ $user->update([
'first_name' => $validData['first_name'], 'first_name' => $validData['first_name'],
'last_name' => $validData['last_name'], 'last_name' => $validData['last_name'],

View File

@ -24,6 +24,7 @@
value="{{ $user->judging_preference }}"/> value="{{ $user->judging_preference }}"/>
<x-form.select name="school_id" colspan="6"> <x-form.select name="school_id" colspan="6">
<x-slot:label>School</x-slot:label> <x-slot:label>School</x-slot:label>
<option value="">No School</option>
@foreach ($schools as $school) @foreach ($schools as $school)
<option value="{{ $school->id }}" <option value="{{ $school->id }}"
@if ($user->school_id == $school->id) selected @endif>{{ $school->name }}</option> @if ($user->school_id == $school->id) selected @endif>{{ $school->name }}</option>

View File

@ -5,6 +5,7 @@ use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use function Pest\Laravel\actingAs; use function Pest\Laravel\actingAs;
use function Pest\Laravel\assertDatabaseHas;
use function Pest\Laravel\delete; use function Pest\Laravel\delete;
use function Pest\Laravel\get; use function Pest\Laravel\get;
use function Pest\Laravel\patch; use function Pest\Laravel\patch;
@ -171,6 +172,31 @@ it('allows an administrator to modify a user', function () {
->assertSee($newData['judging_preference']) ->assertSee($newData['judging_preference'])
->assertSee($newSchool->name); ->assertSee($newSchool->name);
}); });
it('allows a users school to be set to no school', function () {
// Arrange
$school = School::factory()->create();
$user = User::factory()->create(['school_id' => $school->id]);
$newData = [
'first_name' => 'New First Name',
'last_name' => 'New Last Name',
'email' => 'new@emailllllll.com',
'cell_phone' => '123-456-7890',
'judging_preference' => 'New Judging Preference',
'school_id' => '',
];
actAsAdmin();
// Act & Assert
$response = patch(route('admin.users.update', $user), $newData);
/** @noinspection PhpUnhandledExceptionInspection */
$response
->assertSessionHasNoErrors()
->assertRedirect(route('admin.users.index'));
// Assert DB has user id with null school
assertDatabaseHas('users', [
'id' => $user->id,
'school_id' => null,
]);
});
it('has a delete link for the user if not the current user', function () { it('has a delete link for the user if not the current user', function () {
// Arrange // Arrange
actingAs($this->adminUser); actingAs($this->adminUser);