dongtazu3080 2018-02-27 13:00 采纳率: 100%
浏览 37

以相同的形式/页面更新和创建的方法

I have a form to edit the administrators of a post.

This form has some radio buttons. Each radio button corresponds to a administrator of a a post. When some radio button (administrator) is clicked the details of the administrator are populated in the form fields. When the "Update" button is clicked the details of the administrator are updated.

This is working fine.

Doubt:

But there is a static radio button, "Create new administrator", that allows to create a new administrator. When this radio button is selected the form fields are reseted so the user can insert values to create a new admin.

My doubt is how we can reuse this same form for the udpate, that is already working fine, but also for the creation of a new administrator when the radio button selected is "Create new admin"? Or, what could be a proper approach for this context?

form:

<form method="post" class="clearfix" action="{{route('admins.update', ['id' => $post->id])}}" enctype="multipart/form-data">
    {{csrf_field()}}
    @foreach($administrators $admin)
          <div class="form-check">
            <input class="form-check-input" type="radio" name="radiobutton" id="{{$admin->id}}" value="">
            <label class="form-check-label" for="">
              {{$admin->name}}
            </label>
          </div>
    @endforeach
    <div class="form-check">
      <input class="form-check-input" type="radio" name="radiobutton" id="create_administrator"
             value="option2">
      <label class="form-check-label" for="exampleRadios2">
          Create new administrator
      </label>
  </div>
  <div class="form-group">
    <label for="name">Name</label>
    <input type="text" required class="form-control" value="{{ $admin->name }}" name="name">
  </div>

  <!-- below I have more form fields like administrator name, email, etc -->

  <input type="submit" id="adminStoreButton" class="btn btn-primary" value="Create"/>
  <input type="submit" id="adminUpdateButton" class="btn btn-primary" value="Update"/>
  </form>

JS: to show the details of the admin based on radio button selection and to hide and show the store/update button

var admins = {!!  $administrators !!}

$("input[name='radiobutton']").click(function() {

if($(this).attr("id") == "create_administrator"){
$("#adminUpdateButton").hide();
$("#adminStoreButton").show();
form.attr('action', '{{route('admins.store', ['post_id' => $post->id])}}');
}
else{
$("#adminUpdateButton").show();
$("#adminStoreButton").hide();
form.attr('action', '{{route('admins.update', ['post_id' => $post->id])}}');
}

let id = $(this).attr("id");

let data = administrators.find(e => e.id == id) || {
name: "",
email: "",
...: ""
};
$("input[name='name']").val(data.name);
...
});

// update admins routes

Route::get('post/edit/{id}/admins',    [ 'uses' => 'AdminController@edit', 'as'=>'admins.edit']);
Route::post('post/update/{id}/admins', [ 'uses' => 'AdminController@update', 'as'=>'admins.update']);

Administrator controller update method:

    public function update(Request $request, $id){


        $this->validate($request, [
            'name' => 'required|string',
          ...
        ]);


        $adminToUpdate = Administrator::find($request->radiobutton);

        $adminToUpdate->name = $request->name;
        ...

        $adminToUpdate->save();

        return redirect()->back();
    }

}

Administrator controller edit method:

public function edit($id)
    {
        $post = Post::find($id);
        $administrators = Administrator::where('post_id', $id)->get();

        return view('administrators.edit')
            ->with('post', $post)
            ->with('administrators', $administrators));
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?