2017-09-01 42 views
1

我有一个表单。提交表单后,如果有一些错误,那么它会重定向到同一页面并预先填充用户提交的数据。我有这种形式的3 checkbox字段。假设用户只检查了first checkbox并提交了表单。所以如果表单有一些错误,它会重定向到同一页面,但是问题是它再次在这里重定向,然后all the checkboxes got selected instead of only the 1st oneLaravel复选框输入旧的不能正常工作

<div class="form-group"> 
    <label>Hobbies: <span><em>{{$errors->first('hobbies')}}</em></span></label> 
    <input type="checkbox" name="hobbies[]" value="Cricket" @if (is_array(old('hobbies'))) && (in_array('Cricket', old('hobbies'))) checked @endif> Cricket 
    <input type="checkbox" name="hobbies[]" value="Football" @if (is_array(old('hobbies'))) && (in_array('Football', old('hobbies'))) checked @endif> Football 
    <input type="checkbox" name="hobbies[]" value="Badminton" @if (is_array(old('hobbies'))) && (in_array('Badminton', old('hobbies'))) checked @endif> Badminton 
</div> 
+0

有你输入::旧的(? – rahulsm

+0

没有。我没有检查我使用laravel 5.4 –

+0

其中laravel的版本,您正在使用? – rahulsm

回答

0

你应该这样做的方式,使其工作,

<div class="form-group"> 
    <label>Hobbies: <span><em>{{$errors->first('hobbies')}}</em></span></label> 
    <input type="checkbox" name="hobbies[]" value="Cricket" {{ !empty(old('hobbies')) && (in_array('Cricket', old('hobbies'))) ? 'checked' : '' }}> Cricket 
    <input type="checkbox" name="hobbies[]" value="Cricket" {{ !empty(old('hobbies')) && (in_array('Football', old('hobbies'))) ? 'checked' : '' }}> Football 
    <input type="checkbox" name="hobbies[]" value="Cricket" {{ !empty(old('hobbies')) && (in_array('Badminton', old('hobbies'))) ? 'checked' : '' }}> Badminton 
</div> 

给它一个尝试,它应该工作。

1

你有不平衡和错位的圆括号,所以你的if是不正确的评估。

@if (is_array(old('hobbies')) && in_array('Badminton', old('hobbies'))) 
+0

是的,你是对的。 –