0

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?

1
  • are you using same form for both create and edit ? Commented Mar 27, 2018 at 13:14

1 Answer 1

2

You can use the old() helper to achieve this:

<input class="form-check-input radio" type="radio" name="radiobutton" 
     value="{{ $post->id }}" id="{{ $post->id }}"
     {{ old('radiobutton') ? 'checked' : ''}}>

This will print checked inside the input tag if its found in the old request. For further information check the Laravel documentation about this subject.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but its not working. It works in the case of create a new post, but in case of update an existing post it dont works. For example a existing post "post 1 title" is checked, the form is populated with that post info, one of the fields is a date and a invalid date is introduced and "update" button is clicked, it appears an validation error and the "create post" radio button becomes checked instead of the radio button that corresponds to the post"post 1 test".
Is to put " {{ old('radiobutton') ? 'checked' : ''}}" in each existing post input and also in the input that corresponds to "create new post"?
@AdamK I'm sorry but I'm having a hard time understanding you, your explanation might be clear with the result in front on you, but I cannot figure out what you're talking about. The thing is, that code allows you to detect past checked radios and check them, now just change it to suit your application needs.
This always leaves the final radio checked if there are multiple radio button. You also need to compare the value too: {{ old('program') == $program->id ? 'checked' : ''}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.