如何解决在使用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()
帮助程序方法支持在验证失败时检索任何旧表单值。此外,它允许在empty / null上传递默认值,这意味着在最初调用编辑页面时,我们可以从数据库传递默认值。
<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();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。