mirror of https://github.com/kcal-app/kcal.git
Improve Food validation feedback
This commit is contained in:
parent
9360997bee
commit
0982ac1601
|
@ -2,13 +2,12 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\UpdateFoodRequest;
|
||||
use App\Models\Food;
|
||||
use App\Rules\StringIsDecimalOrFraction;
|
||||
use App\Support\Number;
|
||||
use App\Support\Nutrients;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
|
@ -35,7 +34,7 @@ class FoodController extends Controller
|
|||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
public function store(UpdateFoodRequest $request): RedirectResponse
|
||||
{
|
||||
return $this->update($request, new Food());
|
||||
}
|
||||
|
@ -72,25 +71,10 @@ class FoodController extends Controller
|
|||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Food $food): RedirectResponse
|
||||
public function update(UpdateFoodRequest $request, Food $food): RedirectResponse
|
||||
{
|
||||
$attributes = $request->validate([
|
||||
'name' => 'required|string',
|
||||
'detail' => 'nullable|string',
|
||||
'brand' => 'nullable|string',
|
||||
'source' => 'nullable|string',
|
||||
'notes' => 'nullable|string',
|
||||
'serving_size' => ['required', new StringIsDecimalOrFraction],
|
||||
'serving_unit' => 'nullable|string',
|
||||
'serving_unit_name' => 'nullable|string',
|
||||
'serving_weight' => 'required|numeric',
|
||||
'calories' => 'nullable|numeric',
|
||||
'fat' => 'nullable|numeric',
|
||||
'cholesterol' => 'nullable|numeric',
|
||||
'sodium' => 'nullable|numeric',
|
||||
'carbohydrates' => 'nullable|numeric',
|
||||
'protein' => 'nullable|numeric',
|
||||
]);
|
||||
$attributes = $request->validated();
|
||||
|
||||
$attributes['serving_size'] = Number::floatFromString($attributes['serving_size']);
|
||||
$attributes['name'] = Str::lower($attributes['name']);
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Rules\StringIsDecimalOrFraction;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateFoodRequest extends FormRequest
|
||||
{
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string'],
|
||||
'detail' => ['nullable', 'string'],
|
||||
'brand' => ['nullable', 'string'],
|
||||
'source' => ['nullable', 'string'],
|
||||
'notes' => ['nullable', 'string'],
|
||||
'serving_size' => ['required', new StringIsDecimalOrFraction],
|
||||
'serving_unit' => ['nullable', 'string'],
|
||||
'serving_unit_name' => ['nullable', 'string'],
|
||||
'serving_weight' => ['required', 'numeric', 'min:0'],
|
||||
'calories' => ['nullable', 'numeric', 'min:0'],
|
||||
'fat' => ['nullable', 'numeric', 'min:0'],
|
||||
'cholesterol' => ['nullable', 'numeric', 'min:0'],
|
||||
'sodium' => ['nullable', 'numeric', 'min:0'],
|
||||
'carbohydrates' => ['nullable', 'numeric', 'min:0'],
|
||||
'protein' => ['nullable', 'numeric', 'min:0'],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
|
@ -1,3 +1,20 @@
|
|||
@props(['disabled' => false])
|
||||
@props(['disabled' => false, 'hasError' => false])
|
||||
|
||||
<input {{ $disabled ? 'disabled' : '' }} {!! $attributes->merge(['class' => 'rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50']) !!}>
|
||||
@php
|
||||
$classes = [
|
||||
'rounded-md',
|
||||
'shadow-sm',
|
||||
'border-gray-300',
|
||||
'focus:border-indigo-300',
|
||||
'focus:ring',
|
||||
'focus:ring-indigo-200',
|
||||
'focus:ring-opacity-50'
|
||||
];
|
||||
if ($hasError) {
|
||||
$classes[] = 'border-red-600';
|
||||
}
|
||||
@endphp
|
||||
|
||||
<input
|
||||
{{ $disabled ? 'disabled' : '' }}
|
||||
{!! $attributes->merge(['class' => implode(' ', $classes)]) !!}>
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
class="block mt-1 w-full"
|
||||
autocapitalize="none"
|
||||
:value="old('name', $food->name)"
|
||||
required/>
|
||||
:hasError="$errors->has('name')"/>
|
||||
</div>
|
||||
|
||||
<!-- Detail -->
|
||||
|
@ -53,6 +53,7 @@
|
|||
class="block mt-1 w-full"
|
||||
size="10"
|
||||
:value="old('serving_size', $food->serving_size_formatted)"
|
||||
:hasError="$errors->has('serving_size')"
|
||||
required/>
|
||||
</div>
|
||||
|
||||
|
@ -91,6 +92,7 @@
|
|||
class="block mt-1 w-full"
|
||||
size="10"
|
||||
:value="old('serving_weight', $food->serving_weight)"
|
||||
:hasError="$errors->has('serving_weight')"
|
||||
required/>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -106,7 +108,8 @@
|
|||
type="number"
|
||||
step="any"
|
||||
class="block w-full mt-1 md:w-5/6"
|
||||
:value="old($nutrient['value'], $food->{$nutrient['value']})"/>
|
||||
:value="old($nutrient['value'], $food->{$nutrient['value']})"
|
||||
:hasError="$errors->has($nutrient['value'])"/>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue