mirror of https://github.com/kcal-app/kcal.git
Improve Goal validation feedback
This commit is contained in:
parent
ff5661fdf1
commit
6b68d61385
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\UpdateGoalRequest;
|
||||
use App\Models\Goal;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
|
@ -40,7 +41,7 @@ class GoalController extends Controller
|
|||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
public function store(UpdateGoalRequest $request): RedirectResponse
|
||||
{
|
||||
return $this->update($request, new Goal());
|
||||
}
|
||||
|
@ -70,15 +71,9 @@ class GoalController extends Controller
|
|||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Goal $goal): RedirectResponse
|
||||
public function update(UpdateGoalRequest $request, Goal $goal): RedirectResponse
|
||||
{
|
||||
$attributes = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'frequency' => ['required', 'string'],
|
||||
'name' => ['required', 'string'],
|
||||
'goal' => ['required', 'numeric'],
|
||||
]);
|
||||
$attributes = $request->validated();
|
||||
$goal->fill($attributes)->user()->associate(Auth::user());
|
||||
$goal->save();
|
||||
session()->flash('message', "Goal updated!");
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Rules\StringIsPositiveDecimalOrFraction;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateGoalRequest extends FormRequest
|
||||
{
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'frequency' => ['required', 'string'],
|
||||
'name' => ['required', 'string'],
|
||||
'goal' => ['required', 'numeric', 'min:0'],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
|
@ -6,6 +6,7 @@ use App\Support\Nutrients;
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* App\Models\Goal
|
||||
|
@ -44,7 +45,10 @@ final class Goal extends Model
|
|||
* Supported options for thr frequency attribute.
|
||||
*/
|
||||
public static array $frequencyOptions = [
|
||||
'daily' => ['value' => 'daily', 'label' => 'daily'],
|
||||
'daily' => [
|
||||
'value' => 'daily',
|
||||
'label' => 'daily'
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -94,7 +98,7 @@ final class Goal extends Model
|
|||
foreach (Nutrients::all() as $nutrient) {
|
||||
$options[$nutrient['value']] = [
|
||||
'value' => $nutrient['value'],
|
||||
'label' => $nutrient['label'],
|
||||
'label' => Str::ucfirst($nutrient['label']),
|
||||
'unit' => $nutrient['unit'],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -15,7 +15,8 @@
|
|||
<x-inputs.input name="from"
|
||||
type="date"
|
||||
class="block w-full"
|
||||
:value="old('from', $goal->from?->toDateString())" />
|
||||
:value="old('from', $goal->from?->toDateString())"
|
||||
:hasError="$errors->has('from')" />
|
||||
</div>
|
||||
|
||||
<!-- To -->
|
||||
|
@ -24,7 +25,8 @@
|
|||
<x-inputs.input name="to"
|
||||
type="date"
|
||||
class="block w-full"
|
||||
:value="old('to', $goal->to?->toDateString())" />
|
||||
:value="old('to', $goal->to?->toDateString())"
|
||||
:hasError="$errors->has('to')" />
|
||||
</div>
|
||||
|
||||
<!-- Frequency -->
|
||||
|
@ -33,7 +35,8 @@
|
|||
<x-inputs.select name="frequency"
|
||||
class="block w-full"
|
||||
:options="$frequencyOptions"
|
||||
:selectedValue="old('frequency', $goal->frequency)">
|
||||
:selectedValue="old('frequency', $goal->frequency)"
|
||||
:hasError="$errors->has('frequency')">
|
||||
</x-inputs.select>
|
||||
</div>
|
||||
|
||||
|
@ -44,6 +47,7 @@
|
|||
class="block w-full"
|
||||
:options="$nameOptions"
|
||||
:selectedValue="old('name', $goal->name)"
|
||||
:hasError="$errors->has('name')"
|
||||
required>
|
||||
</x-inputs.select>
|
||||
</div>
|
||||
|
@ -56,6 +60,7 @@
|
|||
step="any"
|
||||
class="block w-full"
|
||||
:value="old('goal', $goal->goal)"
|
||||
:hasError="$errors->has('goal')"
|
||||
required />
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue