Creating student works. Need to work on validation rules
This commit is contained in:
parent
dac1e96330
commit
3ef579d5ed
|
|
@ -5,11 +5,13 @@ namespace App\Http\Controllers;
|
||||||
use App\Models\School;
|
use App\Models\School;
|
||||||
use App\Models\Student;
|
use App\Models\Student;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\Rules\UniqueFullNameAtSchool;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use function abort;
|
use function abort;
|
||||||
use function redirect;
|
use function redirect;
|
||||||
|
|
||||||
|
// TODO validation rules to make sure a student name is unique at a school
|
||||||
class StudentController extends Controller
|
class StudentController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|
@ -37,7 +39,7 @@ class StudentController extends Controller
|
||||||
if ($request->user()->cannot('create', Student::class)) abort(403);
|
if ($request->user()->cannot('create', Student::class)) abort(403);
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'first_name' => ['required'],
|
'first_name' => ['required'],
|
||||||
'last_name' => ['required'],
|
'last_name' => ['required', new UniqueFullNameAtSchool(request('first_name'),request('last_name'), Auth::user()->school_id)],
|
||||||
'grade' => ['required', 'integer'],
|
'grade' => ['required', 'integer'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Rules;
|
||||||
|
|
||||||
|
use App\Models\Student;
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
|
||||||
|
class UniqueFullNameAtSchool implements ValidationRule
|
||||||
|
{
|
||||||
|
protected $first_name;
|
||||||
|
protected $last_name;
|
||||||
|
protected $school_id;
|
||||||
|
|
||||||
|
public function __construct($firstName, $lastName, $schoolID)
|
||||||
|
{
|
||||||
|
$this->first_name = $firstName;
|
||||||
|
$this->last_name = $lastName;
|
||||||
|
$this->school_id = $schoolID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function passes($attributies, $value)
|
||||||
|
{
|
||||||
|
return Student::where('first_name', $this->first_name)
|
||||||
|
->where('last_name', $this->last_name)
|
||||||
|
->where('school_id', $this->school_id)
|
||||||
|
->exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function message()
|
||||||
|
{
|
||||||
|
return "There is already a student with that name at the school you are trying to add them to";
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Run the validation rule.
|
||||||
|
*
|
||||||
|
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
|
||||||
|
*/
|
||||||
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue