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;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Requests\UpdateGoalRequest;
|
||||||
use App\Models\Goal;
|
use App\Models\Goal;
|
||||||
use Illuminate\Contracts\View\View;
|
use Illuminate\Contracts\View\View;
|
||||||
use Illuminate\Http\RedirectResponse;
|
use Illuminate\Http\RedirectResponse;
|
||||||
|
|
@ -40,7 +41,7 @@ class GoalController extends Controller
|
||||||
/**
|
/**
|
||||||
* Store a newly created resource in storage.
|
* 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());
|
return $this->update($request, new Goal());
|
||||||
}
|
}
|
||||||
|
|
@ -70,15 +71,9 @@ class GoalController extends Controller
|
||||||
/**
|
/**
|
||||||
* Update the specified resource in storage.
|
* 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([
|
$attributes = $request->validated();
|
||||||
'from' => ['nullable', 'date'],
|
|
||||||
'to' => ['nullable', 'date'],
|
|
||||||
'frequency' => ['required', 'string'],
|
|
||||||
'name' => ['required', 'string'],
|
|
||||||
'goal' => ['required', 'numeric'],
|
|
||||||
]);
|
|
||||||
$goal->fill($attributes)->user()->associate(Auth::user());
|
$goal->fill($attributes)->user()->associate(Auth::user());
|
||||||
$goal->save();
|
$goal->save();
|
||||||
session()->flash('message', "Goal updated!");
|
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\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* App\Models\Goal
|
* App\Models\Goal
|
||||||
|
|
@ -44,7 +45,10 @@ final class Goal extends Model
|
||||||
* Supported options for thr frequency attribute.
|
* Supported options for thr frequency attribute.
|
||||||
*/
|
*/
|
||||||
public static array $frequencyOptions = [
|
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) {
|
foreach (Nutrients::all() as $nutrient) {
|
||||||
$options[$nutrient['value']] = [
|
$options[$nutrient['value']] = [
|
||||||
'value' => $nutrient['value'],
|
'value' => $nutrient['value'],
|
||||||
'label' => $nutrient['label'],
|
'label' => Str::ucfirst($nutrient['label']),
|
||||||
'unit' => $nutrient['unit'],
|
'unit' => $nutrient['unit'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,8 @@
|
||||||
<x-inputs.input name="from"
|
<x-inputs.input name="from"
|
||||||
type="date"
|
type="date"
|
||||||
class="block w-full"
|
class="block w-full"
|
||||||
:value="old('from', $goal->from?->toDateString())" />
|
:value="old('from', $goal->from?->toDateString())"
|
||||||
|
:hasError="$errors->has('from')" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- To -->
|
<!-- To -->
|
||||||
|
|
@ -24,7 +25,8 @@
|
||||||
<x-inputs.input name="to"
|
<x-inputs.input name="to"
|
||||||
type="date"
|
type="date"
|
||||||
class="block w-full"
|
class="block w-full"
|
||||||
:value="old('to', $goal->to?->toDateString())" />
|
:value="old('to', $goal->to?->toDateString())"
|
||||||
|
:hasError="$errors->has('to')" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Frequency -->
|
<!-- Frequency -->
|
||||||
|
|
@ -33,7 +35,8 @@
|
||||||
<x-inputs.select name="frequency"
|
<x-inputs.select name="frequency"
|
||||||
class="block w-full"
|
class="block w-full"
|
||||||
:options="$frequencyOptions"
|
:options="$frequencyOptions"
|
||||||
:selectedValue="old('frequency', $goal->frequency)">
|
:selectedValue="old('frequency', $goal->frequency)"
|
||||||
|
:hasError="$errors->has('frequency')">
|
||||||
</x-inputs.select>
|
</x-inputs.select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -44,6 +47,7 @@
|
||||||
class="block w-full"
|
class="block w-full"
|
||||||
:options="$nameOptions"
|
:options="$nameOptions"
|
||||||
:selectedValue="old('name', $goal->name)"
|
:selectedValue="old('name', $goal->name)"
|
||||||
|
:hasError="$errors->has('name')"
|
||||||
required>
|
required>
|
||||||
</x-inputs.select>
|
</x-inputs.select>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -56,6 +60,7 @@
|
||||||
step="any"
|
step="any"
|
||||||
class="block w-full"
|
class="block w-full"
|
||||||
:value="old('goal', $goal->goal)"
|
:value="old('goal', $goal->goal)"
|
||||||
|
:hasError="$errors->has('goal')"
|
||||||
required />
|
required />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue