【问题标题】:maintain radio button checked if there are validation errors如果存在验证错误,请保持选中单选按钮
【发布时间】:2018-09-15 09:41:51
【问题描述】:

我有一个视图,其中有一些单选按钮,每个按钮对应于数据库中存在的一个帖子。下面还有一个单选按钮来创建新帖子“创建新帖子”。当页面被访问时,“创建新帖子”单选按钮被选中,因此下面的其他表单字段为空

问题:当用户选择某个与现有帖子对应的单选按钮时,表单字段将使用下面的 jQuery 填充数据库中存储的帖子信息。如果用户更改表单的某些信息,例如日期字段,并引入格式不正确的日期并提交表单,则会出现错误,但“创建帖子”单选按钮被选中,因此而不是用户继续在编辑所选帖子的上下文中,他更改为创建新帖子的上下文,因为单选按钮“创建新帖子”被选中。

你知道如何解决这个问题吗?

带有每个帖子单选按钮和“创建新帖子”单选按钮和其他表单字段的表单:

<p>{{$anyPost}}-aaaaa</p>
<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, 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 根据所选帖子填充表单字段并根据所选单选按钮更改表单操作:

var posts = {!!  $post !!}
$("#postupdate").hide();
$("input[name='radiobutton']").click(function() {
  let id = $(this).attr("id");
  if (id == "create_post") {
    $("#postupdate").hide();
    $("#poststore").show();
    $("#editposts").attr('action', '{{route('posts.store', ['post_id' => $post->id])}}');
    $("input[name='title']").val("");
    ...
  } else {
    $("#postupdate").show();
    $("#poststore").hide();
    $("#editposts").attr('action', '{{route('posts.update', ['post_id' => $post->id])}}');
    let data = posts.find(e => e.id == id) || {
      title: "",
      ...

    };
      $("input[name='title']").val(data.title);
      ...
    }
    }); 

我在控制器中有这个“-&gt;with('anyPost', $isAnyPostButtonChecked);”要调试,表单上方有“&lt;p&gt;{{$anyPost}}-aaaaa&lt;/p&gt;”,但在提交表单之前和之后它总是显示为假。

public function edit($id)
{
    ...
    $isAnyPostButtonChecked = false;
    $isAnyPostButtonChecked = $isAnyPostButtonChecked && (old('radiobutton') && old('radiobutton') == $post->id);
    return view('posts.edit')
        ->with('posts', $post)
        ->with('anyPost', $isAnyPostButtonChecked);
}

“{{var_dump(old('radiobutton')}}”显示:

【问题讨论】:

  • 请使用 sn-p 直观地重现问题,这样您更有可能获得帮助
  • 谢谢,但问题包括 php 代码,因为用户正在更新帖子并提交表单,如果出现验证错误,选中的单选按钮是“创建帖子”单选按钮,所以这个问题,它不仅包括 jquery 和 html 的 php 代码。
  • checked checked 重复
  • 哪个单选按钮没有被选中?一个输入应该只有一个选中的属性,这个{{ (old('radiobutton') &amp;&amp; old('radiobutton') == 'create_post') ? 'checked' : '' }}。我认为您的 laravel 验证没有传递所需的值。
  • 什么是 $event->id

标签: javascript php jquery html laravel


【解决方案1】:

试试这个..

如下更改create_post复选框..

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

checked checked 静态属性被移除,只使用条件属性【也有变化】。如果表单新填充或没有old('radiobutton') 值或old('radiobutton') 值为create_post,则选中create_post 复选框。

同时更新您的调试代码..

public function edit($id, Request $request)
{
    ...
    $isAnyPostButtonChecked = false;
    $isAnyPostButtonChecked = $isAnyPostButtonChecked || !empty($request->get('radiobutton'));
    return view('posts.edit')
        ->with('posts', $post)
        ->with('anyPost', $isAnyPostButtonChecked);
}

希望对您有所帮助..如果有任何问题请告诉我..

【讨论】:

  • 谢谢,帖子更新似乎有效。当帖子更新并且存在验证错误时,单选按钮保持选中。但是当我点击创建新帖子时,插入一些值,然后点击“更新”,它会出现“从空值创建默认对象”。
  • 更新或存储方法?似乎当单击“创建新帖子”单选按钮而不是请求转到存储方法时,它转到更新方法。
  • 公共函数更新(请求 $request, $id){ $this->validate($request, [ 'name' => 'required|string', ... ]); $postUpdate = Post::find($request->radiobutton); $postUpdate->name = $request->name; ... $postUpdate->保存();会话::flash('成功','成功');返回重定向()->返回(); }
  • @AdamK 您的表单似乎指向更新路线。 action="{{route('posts.update', ['post_id' =&gt; $post-&gt;id])}}"。那么它会如何调用create方法呢?
  • 在创建新帖子时提交到posts.create url。使用 Js 或使用其他带有新帖子 url 的表单标签。
【解决方案2】:

您在单选按钮上有两次checked,将其删除,如果条件不成立,则不会检查它。

所以,改变这个:

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

收件人:

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

【讨论】:

  • 谢谢,但同样的问题,提交表单后没有选中单选按钮。
  • @AdamK 你检查了old('radiobutton') 的值吗?
  • 你知道如何检查吗?像这样的“{{old('radiobutton')}}”似乎是空的。
  • {{var_dump(old('radiobutton')}}
  • 它显示为“NULL”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-04
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 2018-11-28
  • 1970-01-01
  • 2013-11-03
相关资源
最近更新 更多