Allow null values for ingredients

This commit is contained in:
Christopher C. Wells 2020-12-30 11:57:08 -08:00
parent d4e3658294
commit f8c6f81e60
6 changed files with 53 additions and 15 deletions

View File

@ -60,11 +60,11 @@ class RecipeController extends Controller
'description' => 'required|string', 'description' => 'required|string',
'servings' => 'required|numeric', 'servings' => 'required|numeric',
'ingredients_amount' => 'required|array', 'ingredients_amount' => 'required|array',
'ingredients_amount.*' => 'required|numeric|min:0', 'ingredients_amount.*' => 'required_with:ingredients.*|nullable|numeric|min:0',
'ingredients_unit' => 'required|array', 'ingredients_unit' => 'required|array',
'ingredients_unit.*' => 'nullable|string', 'ingredients_unit.*' => 'nullable|string',
'ingredients' => 'required|array', 'ingredients' => 'required|array',
'ingredients.*' => 'required|exists:App\Models\Ingredient,id', 'ingredients.*' => 'required_with:ingredients_amount.*|nullable|exists:App\Models\Ingredient,id',
]); ]);
$recipe = new Recipe([ $recipe = new Recipe([
@ -79,7 +79,7 @@ class RecipeController extends Controller
return; return;
} }
$ingredient_amounts = []; $ingredient_amounts = [];
foreach ($input['ingredients_amount'] as $key => $amount) { foreach (array_filter($input['ingredients_amount']) as $key => $amount) {
$ingredient_amounts[$key] = new IngredientAmount([ $ingredient_amounts[$key] = new IngredientAmount([
'amount' => (float) $amount, 'amount' => (float) $amount,
'unit' => $input['ingredients_unit'][$key], 'unit' => $input['ingredients_unit'][$key],

View File

@ -9,17 +9,17 @@ use Illuminate\View\Component;
class Select extends Component class Select extends Component
{ {
public Collection $options; public Collection $options;
public string $selectedValue; public ?string $selectedValue;
/** /**
* Select constructor. * Select constructor.
* *
* @param \Illuminate\Support\Collection $options * @param \Illuminate\Support\Collection $options
* @param string $selectedValue * @param ?string $selectedValue
*/ */
public function __construct( public function __construct(
Collection $options, Collection $options,
string $selectedValue = '', ?string $selectedValue = '',
) { ) {
$this->options = $options; $this->options = $options;
$this->selectedValue = $selectedValue; $this->selectedValue = $selectedValue;

View File

@ -0,0 +1,28 @@
<?php
namespace App\View\Components\Inputs;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\View\Component;
class Textarea extends Component
{
public ?string $value;
/**
* Select constructor.
*
* @param ?string $value
*/
public function __construct(?string $value = '') {
$this->value = $value;
}
public function render(): View
{
return view('components.inputs.textarea')
->with('value', $this->value);
}
}

View File

@ -2,7 +2,7 @@
{{ $slot }} {{ $slot }}
@foreach ($options as $option) @foreach ($options as $option)
<option value="{{ $option['value'] }}" <option value="{{ $option['value'] }}"
@if ($option['value'] === $selectedValue) selected @endif> @if ($option['value'] == $selectedValue) selected @endif>
{{ $option['label'] }} {{ $option['label'] }}
</option> </option>
@endforeach @endforeach

View File

@ -1,4 +1 @@
@props(['disabled' => false]) <textarea {!! $attributes->merge(['class' => 'rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50']) !!}>{{ $value }}</textarea>
<textarea {{ $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']) !!}>
</textarea>

View File

@ -70,19 +70,32 @@
<div class="flex flex-row space-x-4 mb-4"> <div class="flex flex-row space-x-4 mb-4">
<x-inputs.input type="number" <x-inputs.input type="number"
name="ingredients_amount[]" name="ingredients_amount[]"
step="any" :value="old('ingredients_amount.' . $i)"
required /> step="any" />
<x-inputs.select name="ingredients_unit[]" <x-inputs.select name="ingredients_unit[]"
:options="$ingredient_units"> :options="$ingredient_units"
:selectedValue="old('ingredients_unit.' . $i)">
<option value=""></option> <option value=""></option>
</x-inputs.select> </x-inputs.select>
<x-inputs.select name="ingredients[]" <x-inputs.select name="ingredients[]"
:options="$ingredients" :options="$ingredients"
required> :selectedValue="old('ingredients.' . $i)">
<option value=""></option> <option value=""></option>
</x-inputs.select> </x-inputs.select>
</div> </div>
@endfor @endfor
<!-- Steps -->
<h3 class="pt-2 mb-2 font-extrabold">Steps</h3>
@for($i = 1; $i < 6; $i++)
<div class="flex flex-row space-x-4 mb-4">
<div class="text-3xl text-gray-400 text-center">{{ $i }}</div>
<x-inputs.textarea class="block mt-1 w-full"
name="steps[]"
:value="old('steps[$i]')" />
</div>
@endfor
<div class="flex items-center justify-end mt-4"> <div class="flex items-center justify-end mt-4">
<x-inputs.button class="ml-3"> <x-inputs.button class="ml-3">
{{ __('Add') }} {{ __('Add') }}