Ensure tags persist from old form data

This commit is contained in:
Christopher C. Wells 2021-01-31 14:17:11 -08:00
parent fa3c1ee69e
commit cfa9e2c1eb
4 changed files with 20 additions and 4 deletions

View File

@ -53,8 +53,15 @@ class FoodController extends Controller
*/
public function edit(Food $food): View
{
// Convert string tags (from old form data) to a Collection.
$food_tags = old('tags', $food->tags->pluck('name'));
if (is_string($food_tags)) {
$food_tags = new Collection(explode(',', $food_tags));
}
return view('foods.edit')
->with('food', $food)
->with('food_tags', $food_tags)
->with('nutrients', Nutrients::$all)
->with('serving_units', new Collection([
['value' => 'tsp', 'label' => 'tsp.'],

View File

@ -125,8 +125,15 @@ class RecipeController extends Controller
}
}
// Convert string tags (from old form data) to a Collection.
$recipe_tags = old('tags', $recipe->tags->pluck('name'));
if (is_string($recipe_tags)) {
$recipe_tags = new Collection(explode(',', $recipe_tags));
}
return view('recipes.edit')
->with('recipe', $recipe)
->with('recipe_tags', $recipe_tags)
->with('ingredients', $ingredients)
->with('steps', $steps)
->with('ingredients_units', Nutrients::$units);

View File

@ -66,7 +66,8 @@
type="text"
name="serving_size"
size="10"
:value="old('serving_size', $old_value)"/>
:value="old('serving_size', $old_value)"
required/>
</div>
<!-- Serving unit -->
@ -91,7 +92,8 @@
step="any"
name="serving_weight"
size="10"
:value="old('serving_weight', $food->serving_weight)"/>
:value="old('serving_weight', $food->serving_weight)"
required/>
</div>
</div>
@ -113,7 +115,7 @@
</div>
<!-- Tags -->
<x-tagger :defaultTags="$food->tags->pluck('name')"/>
<x-tagger :defaultTags="$food_tags"/>
<!-- Source -->
<div class="flex-auto">

View File

@ -60,7 +60,7 @@
</div>
<!-- Tags -->
<x-tagger :defaultTags="$recipe->tags->pluck('name')"/>
<x-tagger :defaultTags="$recipe_tags"/>
</div>
<!-- Ingredients -->