I have a update method to update a post and a store method to store a post, the update is like below, the store method is similar but to 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();
}
Then in the view i show in each radio button the post name and I also show a radio button to create a new post:
<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>
Then the user can select a radio button and edit a existing post or can select the "create post" radio button to create a new post.
If the user selects the radio button "Create post" and fill some form fields and click "create" without fill the form correctly, for example with a incorrect date format, the fields are not cleared they remain so the user dont need to introduce them again. This works fine.
Issue: But when a user select some radio button that corresponds to a existing post, the form fields are populated with the post stored info in the database using JS. If the user change some info of the form, for example the date, and introduce a date with a incorrect format, it appears an error and the fields are not clear but the "create post" radio button becomes checked, and so instead of the user continue in the context of edit a post he changes for a context to create a new post.
Do you know how to, when a user clicks in a radio button that corresponds to an existing post and introuce some invalid info and clicks in "update" keep with this post radio button checked and not the "create post" radio button checked?