【问题标题】:verify if any edit post radio button is checked when form is submited验证提交表单时是否选中了任何编辑帖子单选按钮
【发布时间】:2018-09-15 07:05:22
【问题描述】:

我有这个方法,我将一些帖子和这个"isAnyPostButtonChecked" 传递给视图:

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

在视图中我有一个表单,在表单上方我有"{{$anyPost}}" 以验证在提交表单时是否选中了任何单选按钮。但它不会在选择单选按钮"{{$anyPost}}"时显示任何内容,并且提交表单。你知道为什么吗?

   {{$anyPost}}
    <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,... -->
  <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="postupdate" value="Update"/>
</form>

【问题讨论】:

  • -&gt;with('postss, $post) - 这应该是-&gt;with('posts, $post),不是吗?
  • 谢谢,这是一个错字。我更新了问题。
  • 你的错误是-&gt;with('posts, $post)应该是-&gt;with('posts', $post)
  • 谢谢,我更新了问题。
  • 你的控制器中有$isAnyPostButtonChecked的值吗?

标签: php laravel


【解决方案1】:

如果不是打印为假,可以查看this answer

{{ $anyPost ? 'true' : 'false' }}

在执行此操作之前,请通过将其他变量分配给 $anyPost 来检查。

由于我的声誉低,我无法将其发布为评论:D

如果不工作请告诉我。

【讨论】:

  • 谢谢,但是当检查编辑后单选按钮并且表单被提交时,它也会显示为false。你知道为什么吗?
  • 据我了解,无论复选框的值是什么,您都会在控制器中获得复选框的值。据我所知,如果我们选中一个复选框并提交表单,它将出现在 dd($request->all()) 中。如果我们不选中该复选框,它将不会出现在控制器表单值中。因此,您可以检查是否通过 isset($request['radiobutton']) 检查了复选框。还有一件事,我认为生成具有相同名称的多个输入不是一个好习惯。你可以检查这个答案stackoverflow.com/questions/2203430/…
【解决方案2】:

因为 $anyPost 变量每次都包含 false,所以在 false 值视图上显示注释,如果有 true 值,则视图中显示 1。

您的情况有问题,请检查 true 值。

【讨论】:

    猜你喜欢
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2014-04-13
    • 2013-04-10
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    相关资源
    最近更新 更多