From f8c6f81e60feb62985c725eb35227108d0df1de9 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 30 Dec 2020 11:57:08 -0800 Subject: [PATCH] Allow null values for ingredients --- app/Http/Controllers/RecipeController.php | 6 ++-- app/View/Components/Inputs/Select.php | 6 ++-- app/View/Components/Inputs/Textarea.php | 28 +++++++++++++++++++ .../views/components/inputs/select.blade.php | 2 +- .../components/inputs/textarea.blade.php | 5 +--- resources/views/recipes/create.blade.php | 21 +++++++++++--- 6 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 app/View/Components/Inputs/Textarea.php diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index c742651..e4dabf7 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -60,11 +60,11 @@ class RecipeController extends Controller 'description' => 'required|string', 'servings' => 'required|numeric', '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.*' => 'nullable|string', 'ingredients' => 'required|array', - 'ingredients.*' => 'required|exists:App\Models\Ingredient,id', + 'ingredients.*' => 'required_with:ingredients_amount.*|nullable|exists:App\Models\Ingredient,id', ]); $recipe = new Recipe([ @@ -79,7 +79,7 @@ class RecipeController extends Controller return; } $ingredient_amounts = []; - foreach ($input['ingredients_amount'] as $key => $amount) { + foreach (array_filter($input['ingredients_amount']) as $key => $amount) { $ingredient_amounts[$key] = new IngredientAmount([ 'amount' => (float) $amount, 'unit' => $input['ingredients_unit'][$key], diff --git a/app/View/Components/Inputs/Select.php b/app/View/Components/Inputs/Select.php index f3e5b83..d0aa996 100644 --- a/app/View/Components/Inputs/Select.php +++ b/app/View/Components/Inputs/Select.php @@ -9,17 +9,17 @@ use Illuminate\View\Component; class Select extends Component { public Collection $options; - public string $selectedValue; + public ?string $selectedValue; /** * Select constructor. * * @param \Illuminate\Support\Collection $options - * @param string $selectedValue + * @param ?string $selectedValue */ public function __construct( Collection $options, - string $selectedValue = '', + ?string $selectedValue = '', ) { $this->options = $options; $this->selectedValue = $selectedValue; diff --git a/app/View/Components/Inputs/Textarea.php b/app/View/Components/Inputs/Textarea.php new file mode 100644 index 0000000..7006db8 --- /dev/null +++ b/app/View/Components/Inputs/Textarea.php @@ -0,0 +1,28 @@ +value = $value; + } + + public function render(): View + { + return view('components.inputs.textarea') + ->with('value', $this->value); + } + +} diff --git a/resources/views/components/inputs/select.blade.php b/resources/views/components/inputs/select.blade.php index c624561..e4f481e 100644 --- a/resources/views/components/inputs/select.blade.php +++ b/resources/views/components/inputs/select.blade.php @@ -2,7 +2,7 @@ {{ $slot }} @foreach ($options as $option) @endforeach diff --git a/resources/views/components/inputs/textarea.blade.php b/resources/views/components/inputs/textarea.blade.php index c6f61d3..ebb3c95 100644 --- a/resources/views/components/inputs/textarea.blade.php +++ b/resources/views/components/inputs/textarea.blade.php @@ -1,4 +1 @@ -@props(['disabled' => false]) - - + diff --git a/resources/views/recipes/create.blade.php b/resources/views/recipes/create.blade.php index 66efaef..519d393 100644 --- a/resources/views/recipes/create.blade.php +++ b/resources/views/recipes/create.blade.php @@ -70,19 +70,32 @@
+ :value="old('ingredients_amount.' . $i)" + step="any" /> + :options="$ingredient_units" + :selectedValue="old('ingredients_unit.' . $i)"> + :selectedValue="old('ingredients.' . $i)">
@endfor + + +

Steps

+ @for($i = 1; $i < 6; $i++) +
+
{{ $i }}
+ +
+ @endfor +
{{ __('Add') }}