php - 使用 laravel 通过更新请求验证后,如何维护复选框中的旧输入值?
问题描述
当我打开编辑页面时,它应该具有来自数据库的值,当我更改值时,如果验证失败,我需要保持 {{old('input')}} 值。这是代码:
<input type="checkbox" id="show_in_website" name="show_in_website" @if($data->show_in_website=='1') checked @endif>Show in Website
解决方案
您可以使用old()
辅助方法。辅助方法支持在old()
验证失败时检索任何旧表单值。此外,它允许在空/空上传递默认值,这意味着我们可以在最初调用编辑页面时将数据库中的值作为默认值传递。
<input type="checkbox" id="show_in_website" name="show_in_website" {{ old('show_in_website', $data->show_in_website) ? 'checked' : '' }}> Show in Website
另外,请确保您使用以下方式重定向回表单withInput()
:
if($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
推荐阅读
- c# - 如何在 net core C# 中获取当前环境 (QA/Prod/DEV) 名称?
- excel - 使用 vlookup 将数据保存到特定单元格
- haskell - `>||<` 运算符在 Haskell 中的作用是什么?
- jwt - 如何使用 WSO2 API Manager 更改 jwt 的受众
- php - 如何使用漂亮干净的 URL 进行重定向?
- c# - JSON数组响应REST API,提取数据时出错c#
- python-3.x - 使用 GraphQL Body 在 python3 中发布请求
- listview - UWP:在 ListView 中重新排序项目时使用 Move 而不是 Remove+Insert
- java - Hash Map 通过单独的链接(int 键,LinkedList 值)实现链表,对吗?
- asp.net - 如何使用 View 中的下拉列表将数据从 ViewModel 插入数据库