laravel 带有“Input::old()”的未选中复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16884032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:51:49  来源:igfitidea点击:

unchecked checkbox with "Input::old()"

checkboxlaravellaravel-4

提问by Guy Incognito

I'm creating a checkbox in a view:

我正在视图中创建一个复选框:

Form::checkbox('test', 'true', Input::old('test', true));

After unchecking the box, submitting the form and being redirected back:

取消选中该框后,提交表单并被重定向回:

return Redirect::back()->withInput()->withErrors($validation);

The checkbox is still checked - presumably because 'test'doesn't exist in "Input::old"so it is reverting back to the default value.

该复选框仍处于选中状态 - 大概是因为'test'不存在,"Input::old"因此它正在恢复为默认值。

Any suggestions on how best to achieve this?

关于如何最好地实现这一目标的任何建议?

Edit:

编辑:

I've come up with a somewhat hacky way of getting the results I want:

我想出了一种获得我想要的结果的有点hacky的方法:

$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);

I'll write this into a function for reuse, but it seems a bit painful so any comments/suggestions would be great

我会把它写成一个函数以供重用,但这似乎有点痛苦,所以任何评论/建议都会很棒

Edit 2Sorry if not clear - to clarify:

编辑 2对不起,如果不清楚 - 澄清:

  1. Form is loaded with checkbox checked
  2. I uncheck the checkbox and submit the form
  3. The form doesn't validate so I'm returned to the form
  4. The checkbox has re-checked itself - I would expect it to be unchecked, as per the user submitted data
  1. 表单已加载并选中复选框
  2. 我取消选中复选框并提交表单
  3. 表单未通过验证,因此我返回到表单
  4. 该复选框已重新检查 - 根据用户提交的数据,我希望它不会被选中

I assume this happens because if a checkbox isn't checked the element isn't included in the post data

我认为发生这种情况是因为如果未选中复选框,则发布数据中不包含该元素

回答by GTCrais

I had the same problem. Turns out it was because I followed the instructions on how to post an unchecked checkbox, which said:

我有同样的问题。原来这是因为我按照有关如何发布未选中复选框的说明进行操作,其中说:

echo Form::hidden('name', 0);
echo Form::checkbox('name', 1);

The problem with this is that on form validation fail, if the checkbox was checked, Laravel will repopulate both 'hidden' and 'checkbox' fields with old value, so your hidden field will always be submitting 1 instead of 0. Simple solution is just write HTML for the hidden field instead of using the form helper:

问题在于表单验证失败时,如果复选框被选中,Laravel 将使用旧值重新填充“隐藏”和“复选框”字段,因此您的隐藏字段将始终提交 1 而不是 0。简单的解决方案就是为隐藏字段编写 HTML,而不是使用表单助手:

echo '<input type="hidden" name="name" value="0">';
echo Form::checkbox('name', 1);

回答by Antonio Carlos Ribeiro

This should is sufficient

这应该足够了

echo Form::checkbox('test', 'true', Input::old('test', true));

If Input::old() doesn't exists, it will return true, your default.

如果 Input::old() 不存在,它将返回 true,您的默认值。

回答by kablamus

The checkbox and session mismatch was an error that has been corrected in Laravel 4. The following code should preserve the checkbox on redirect:

复选框和会话不匹配是 Laravel 4 中已更正的错误。以下代码应保留重定向时的复选框:

echo Form::checkbox('payment_method', 'credit');

For grouped checkboxes, use something like this:

对于分组复选框,请使用以下内容:

echo Form::checkbox('payment_method[]', 'credit');

That should also work.

这也应该有效。

**Obviously you will want to replace payment_method and credit with your own attributes.

**显然,您将希望用您自己的属性替换 payment_method 和 credit。

回答by ericbae

This was a bit confusing for me too. But this is what I did.

这对我来说也有点混乱。但这就是我所做的。

In my controller,

在我的控制器中,

Session::flash('test', Input::get('test') ? 'true' : 'false');

And in the view

并且在视图中

<?php $test = Session::get('test'); ?>
<input type="checkbox" id="test" name="test" value="true" <?php echo is_null($test) ? 'checked' : ($test == 'true' ? 'checked' : ''); ?> />

So, pretty much using an additional session flash data to get around.

因此,几乎使用额外的会话闪存数据来绕过。

回答by C-A

My solution to the same problem:

我对同样问题的解决方案:

1) Remove the checkbox from validation. Logically, it makes no difference.

1) 从验证中删除复选框。从逻辑上讲,这没有区别。

2) In my controller update function, I added an if statement that used isset to determine whether the value was sent or not (if not sent, it wasn't checked and therefore false).

2) 在我的控制器更新函数中,我添加了一个 if 语句,该语句使用 isset 来确定值是否已发送(如果未发送,则未检查,因此为 false)。

if ($validation->passes())
{
    $xxx = $this->xxx->find($id);
    $xxx->update($input);
    if (!isset($input['checkbox_attribute'])) {
        $xxx->checkbox_attribute = false;
    }
    $xxx->save();
    return Redirect::to('xxx.show');
}

回答by Lucky Soni

Why not do something like this:

为什么不做这样的事情:

<?php $last_turnaround = (array) Input::old('turnaround'); ?>

<?php $last_turnaround = (array) Input::old('turnaround'); ?>

{{ Form::checkbox( 'turnaround', 'normal', in_array('normal', $last_turnaround), array('id' => 'turnaround_normal', 'class' => 'turnaround')) }}

{{ Form::checkbox( 'turnaround', 'normal', in_array('normal', $last_turnaround), array('id' => 'turnaround_normal', 'class' => 'turnaround')) }}

回答by Elias Mamalias Jr.

I have the same problem..

我也有同样的问题..

I fixed it like this

我是这样修的

Form::checkbox('test', 'true', Input::old() ? Input::old('test', false) : true);

hope that helps..

希望有帮助..

回答by Kazuya Gosho

I had the same problem. I end up with creating this helper function:

我有同样的问题。我最终创建了这个辅助函数:

function is_checked(string $name, $value, $default) : string
{
    $current_defaults = Input::old() ? old($name, 0) : $default;
    return ($value == $current_defaults) ? 'checked' : '';
}

<input type="checkbox" name="is_acrive" value="1" 
       {{ is_checked('is_active', 1, $user->is_active) }}>

Multiple checkbox version:

多复选框版本:

function is_checked_multiple(string $name, $value, $defaults) : string
{
    $current_defaults = Input::old() ? old($name, []) : (array)$defaults;
    return in_array($value, $current_defaults) ? 'checked' : '';
}

@foreach ($plans as $plan)
    <input type="checkbox" name="plans[]" value="{{ $plan->id }}" 
           {{ is_checked_multiple('plans', $plan->id, $user->plans->pluck('id') }}>
@endforeach