在laravel 5中验证后重新填充表单中的复选框字段

问题描述 投票:0回答:3

我试图在Laravel中验证后重新填充复选框字段。实际上,复选框将name作为数组。例:

<form name="" action="" method="post">
<div class="row>
   <input type="checkbox" name="models[]" value="1">Model 1
   <input type="checkbox" name="models[]" value="2">Model 2
   <input type="checkbox" name="models[]" value="3">Model 3
   <input type="checkbox" name="models[]" value="4">Model 4
   <input type="checkbox" name="models[]" value="5">Model 5
</div>
<div class="row>
   <input type="checkbox" name="model_name" value="" placeholder="Model Name">       
</div>
<div class="row>
<input type="submit" name="submit" value="submit">
</div>
</form>

我的表格看起来像这样。因此,在提交表单后会出现验证错误,我需要在验证错误后重新填充复选框值。我已经尝试使用{{old('field_name')}}将复选框设置为选中状态。但它没有用。任何人都有解决此问题的解决方案?

validation checkbox laravel-5.4
3个回答
0
投票

我尝试类似于Laravel 5.5中的以下代码

<input type="checkbox" name="models[]" value="1" @if( is_array(old('models')) && in_array(1, old('models'))) checked @endif  >Model 1

0
投票

你很近!如果检测到旧的<input>值,你只需要在model_name元素中加上'checked'一词'重新激活'检查的模式。

<input type="checkbox" name="model_name" value="" placeholder="Model Name">
       @if (old('model_name'))
           checked
       @endif
/>

0
投票

这对我有用(Laravel 5.2)

@foreach ($models as $model)
    <input type="checkbox" name="models[{{ $model->id }}]"
        @if (is_array(old('models')) && in_array($model->id, array_keys(old('models'))))
            checked
        @endif
    > {{ $model->name }}
@endforeach
© www.soinside.com 2019 - 2024. All rights reserved.