未选中复选框,带“Input :: old()”

时间:2013-06-02 15:06:38

标签: checkbox laravel laravel-4

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

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

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

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

仍然会选中该复选框 - 大概是因为'test'"Input::old"不存在,所以它会恢复为默认值。

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

修改

我想出了一个有点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);

我会把它写成一个可以重用的函数,但是看起来有点痛苦所以任何意见/建议都会很棒

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

  1. 表单已加载复选框
  2. 我取消选中该复选框并提交表单
  3. 表单未验证,因此我已返回
  4. 表单
  5. 该复选框已经重新检查了自己 - 根据用户提交的数据,我希望它可以取消选中
  6. 我认为会发生这种情况,因为如果未选中复选框,则该元素不会包含在帖子数据中

8 个答案:

答案 0 :(得分:10)

我遇到了同样的问题。事实证明这是因为我按照说明如何发布未选中的复选框,其中说:

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

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

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

答案 1 :(得分:5)

这应该足够了

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

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

答案 2 :(得分:2)

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

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

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

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

这应该也有效。

**显然你会想要用自己的属性替换payment_method和credit。

答案 3 :(得分:1)

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

在我的控制器中,

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

在视图中

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

所以,几乎使用额外的会话闪存数据来解决问题。

答案 4 :(得分:0)

为什么不这样做:

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

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

答案 5 :(得分:0)

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

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

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');
}

答案 6 :(得分:0)

我有同样的问题..

我像这样修理了

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

希望有所帮助..

答案 7 :(得分:0)

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

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) }}>

多个复选框版本:

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