【发布时间】:2018-09-15 07:05:22
【问题描述】:
我有这个方法,我将一些帖子和这个"isAnyPostButtonChecked" 传递给视图:
public function edit($id)
{
...
$isAnyPostButtonChecked = false;
$isAnyPostButtonChecked = $isAnyPostButtonChecked && (old('radiobutton') && old('radiobutton') == $event->id);
return view('posts.edit')
->with('posts', $post)
->with('anyPost', $isAnyPostButtonChecked);
}
在视图中我有一个表单,在表单上方我有"{{$anyPost}}" 以验证在提交表单时是否选中了任何单选按钮。但它不会在选择单选按钮"{{$anyPost}}"时显示任何内容,并且提交表单。你知道为什么吗?
{{$anyPost}}
<form id="editposts" method="post"
action="{{route('posts.update', ['post_id' => $post->id])}}" enctype="multipart/form-data">
{{csrf_field()}}
<div>
@foreach($posts as $post)
<div class="form-check">
<input {{ (old('radiobutton') && old('radiobutton') == $post->id) ? 'checked' : '' }} class="form-check-input radio" type="radio" name="radiobutton" value="{{ $post->id }}" id="{{$post->id}}">
<label class="form-check-label">
{{$post->title}}
</label>
</div>
@endforeach
</div>
<div class="form-check">
<input checked checked {{ (old('radiobutton') && old('radiobutton') == 'create_post') ? 'checked' : '' }} class="form-check-input" type="radio" name="radiobutton" id="create_post"
value="create_post">
<label class="form-check-label">
Create post
</label>
</div>
<!-- form fields, here is the name but are more like description,... -->
<div class="form-group">
<label>Post title</label>
<input type="text" required class="form-control" value="{{ old('title') }}" name="title" id="tile">
</div>
<input type="submit" id="postupdate" value="Update"/>
</form>
【问题讨论】:
-
->with('postss, $post)- 这应该是->with('posts, $post),不是吗? -
谢谢,这是一个错字。我更新了问题。
-
你的错误是
->with('posts, $post)应该是->with('posts', $post) -
谢谢,我更新了问题。
-
你的控制器中有
$isAnyPostButtonChecked的值吗?