【发布时间】:2018-03-27 11:37:00
【问题描述】:
我有一个更新帖子的update方法和一个存储帖子的store方法,更新如下,store方法类似但要存储:
public function update(Request $request, $id){
$post = post::find($request->radiobutton);
$post->title = $request->title;
// ... other columns
$post->save();
Session::flash('success','post edited with success');
return redirect()->back();
}
然后在视图中,我在每个单选按钮中显示帖子名称,我还显示一个单选按钮来创建新帖子:
<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 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 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, etc -->
<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="poststore" value="Create"/>
<input type="submit" id="postupdate" value="Update"/>
</form>
然后用户可以选择单选按钮并编辑现有帖子,或者可以选择“创建帖子”单选按钮来创建新帖子。
如果用户选择单选按钮“创建帖子”并填写一些表单字段并单击“创建”但未正确填写表单,例如日期格式不正确,则这些字段不会被清除,因此用户不需要再次介绍他们。这很好用。
问题:但是当用户选择某个与现有帖子对应的单选按钮时,表单字段将使用 JS 填充数据库中存储的帖子信息。如果用户更改表单的某些信息,例如日期,并引入格式不正确的日期,则会出现错误并且字段不清晰但“创建帖子”单选按钮被选中,因此而不是用户继续在编辑帖子的上下文中更改他为上下文创建新帖子。
当用户点击与现有帖子对应的单选按钮并引入一些无效信息并点击“更新”时,您知道如何保持选中此帖子单选按钮而不是“创建帖子”单选按钮检查了吗?
【问题讨论】:
-
您是否使用相同的表单来创建和编辑?