我正在视图中创建一个复选框:
Form::checkbox('test', 'true', Input::old('test', true));
取消选中该框后,提交表单并被重定向回来:
return Redirect::back()->withInput()->withErrors($validation);
该复选框仍处于选中状态 - 大概是因为'test'
它不存在,"Input::old"
所以它正在恢复为默认值。
关于如何最好地实现这一目标的任何建议?
编辑:
我想出了一种有点老套的方法来获得我想要的结果:
$isChecked = true; // The default state
// Check if the form has been returned "withInput"
if(Input::old())
{
$isChecked = Input::old('test', false); // If the field has old data available use that, otherwise it is unchecked
}
echo Form::checkbox('test', 'true', $isChecked);
我会将其写入一个函数以供重用,但这似乎有点痛苦,所以任何评论/建议都会很棒
编辑 2 抱歉,如果不清楚 - 澄清:
- 表单已选中复选框
- 我取消选中复选框并提交表单
- 表单未通过验证,因此我返回表单
- 该复选框已重新检查自身 - 根据用户提交的数据,我希望它不会被选中
我认为发生这种情况是因为如果未选中复选框,则该元素不包含在帖子数据中