【问题标题】:Laravel - Repopulate checkbox to ticked or untickedLaravel - 重新填充复选框以勾选或取消勾选
【发布时间】:2015-01-08 06:14:04
【问题描述】:

在 Laravel 中,如果验证失败,它会返回到相同的表单页面。如何重新填充复选框以勾选或取消勾选?

默认情况下,首次加载页面时不勾选。

{{ Form::checkbox('custom_address', false, 
   Input::old('custom_address'), array('class' => 'test')); }}

【问题讨论】:

  • return Redirect::to('form')->withInput();laravel.com/docs/4.2/requests#old-input
  • @NilotpalBarpujari 是的,我已经知道了,return Redirect::to('/order/details')->withErrors($validator)->withInput(Input::all())

标签: php laravel laravel-4 blade


【解决方案1】:

只需使用条件。

@if(!empty(Input::old('custom_adress')))

{{ Form::checkbox('custom_address', true, 
Input::old('custom_address'), array('class' => 'test')); }}

@else

{{ Form::checkbox('custom_address', false, 
Input::old('custom_address'), array('class' => 'test')); }}

@endif

【讨论】:

  • 你不应该像这样复制你的代码。它可能导致维护噩梦。例如,如果有人对代码进行了更改,但没有在两个地方都进行更改,会发生什么?
  • 我认为这就是判断错误。当你看到一个条件时,如果你做出改变,你也必须检查所有其他条件。
【解决方案2】:

您需要为您的复选框设置一个值,否则Input 类将始终将其设为空/未选中​​。

给复选框一个值 1,那么它应该可以工作。

{{ Form::checkbox('custom_address', 1, Input::old('custom_address'), array('class' => 'test')); }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    相关资源
    最近更新 更多