过滤另一个表单中的表单以提交数据

发布于 2025-01-14 01:43:02 字数 3077 浏览 1 评论 0原文

我正在 Laravel 7.4 中工作。我的角色对应于两个不同的“项目”或“区域”,但它们都在同一个名为“角色”的表中,我们可以使用“display_name”字段区分它们,但我希望我的表单创建一个新用户根据所选的“区域”向我显示角色。

我尝试在我的控制器和另一个带有选择的表单中添加一个条件,但由于它位于提交数据以创建用户的表单内,所以这给我带来了麻烦(问题是数据发送时就好像选择是一个提交按钮,所以它给了我一个错误,我必须填写姓名和电子邮件字段,但我不希望这种情况发生)。 我能做些什么?

  • 这是我的观点
<form method="POST" action="{{route('User.store')}}">
  {{ csrf_field() }}
  @include('user.form', ['user'=> new App\User])
  <!--Roles-->
  <br>
  <div class="row justify-content-center text-center">
    <h3 class="txt-unam text-center text-primary">Roles</h3>
  </div>
    
  <div class="my-custom-scrollbar col-md-6">
    <table class="table-scroll table table-sm table-striped ">
      <thead class="txt-grey text-center">
        <tr>
          <th class="th-sticky bg-darker text-center">Role</th>
          <th class="th-sticky bg-darker text-center">Select</th>
        </tr>
      </thead>
      <tbody>
        <form class="row justify-content-center " action="{{url('User')}}">
          <select onchange="this.form.submit()" class="col-sm-6 col-12 form-control" name="id" id="selectRole">
            <option selected value="" name="servescol">Servescol Roles</option>
            <option selected value="" name="indicators">Indicators Roles</option>
          </select>
        </form>
        @foreach($roles as $role)
          <tr>
            <td class="align-middle small text-center">{{$role->display_name}}</td>
            <td class="align-middle text-center"><input type="checkbox" value="{{$role->id}}" id="checkIrRol" name="roles[]" {{$role->pluck('id')->contains($role->id) ? 'checked' : ''}}</td>
          </tr>
        @endforeach
      </tbody>
    </table>
  </div>
  <br>
  <div class="text-center">
    <div class="text-center pt-0">
      <button class="btn btn-success" type="submit" value="Save">Save</button>
      <button class="btn btn-danger" onclick="cancel()" value="Cancel">Cancel</button>
    </div>
  </div>
</form>
  • 这是我的控制器
public function create(Request $request)
{
    if ($request->get('servescol')) {
        $roles = DB::select('SELECT id, role_name FROM roles WHERE id <= 6');
        return view('user.create', compact('roles'));
    }
            
    if ($request->get('indicators')) {
        $roles = DB::select('SELECT id, role_name FROM roles WHERE id > 6');

        return view('user.create', compact('roles'));
    }
    
    $roles = Role::orderby('id')->get();
    $departments = Department::all(['id', 'department_name']);
    
    return view('user.create', compact('roles', 'departments'));
}

顺便说一句,部门与“区域”不同。条件 <= 6> 6根据所属区域对应id。

I'm working in Laravel 7.4. I have roles that correspond to two different "projects" or "areas", but they are all in the same table called "roles" and we can differentiate them with the "display_name" field, but I want my form to create a new User to show me the roles according to the "area" selected.

I've tried adding a condition in my controller and another form with the select, but since it's inside the form that submits the data to create the user, it's giving me trouble (the problem is that the data is sent as if the select were a submit button so it gives me the error that I have to fill in the name and email fields but I don't want that to happen).
What can I do?

  • This is my view
<form method="POST" action="{{route('User.store')}}">
  {{ csrf_field() }}
  @include('user.form', ['user'=> new App\User])
  <!--Roles-->
  <br>
  <div class="row justify-content-center text-center">
    <h3 class="txt-unam text-center text-primary">Roles</h3>
  </div>
    
  <div class="my-custom-scrollbar col-md-6">
    <table class="table-scroll table table-sm table-striped ">
      <thead class="txt-grey text-center">
        <tr>
          <th class="th-sticky bg-darker text-center">Role</th>
          <th class="th-sticky bg-darker text-center">Select</th>
        </tr>
      </thead>
      <tbody>
        <form class="row justify-content-center " action="{{url('User')}}">
          <select onchange="this.form.submit()" class="col-sm-6 col-12 form-control" name="id" id="selectRole">
            <option selected value="" name="servescol">Servescol Roles</option>
            <option selected value="" name="indicators">Indicators Roles</option>
          </select>
        </form>
        @foreach($roles as $role)
          <tr>
            <td class="align-middle small text-center">{{$role->display_name}}</td>
            <td class="align-middle text-center"><input type="checkbox" value="{{$role->id}}" id="checkIrRol" name="roles[]" {{$role->pluck('id')->contains($role->id) ? 'checked' : ''}}</td>
          </tr>
        @endforeach
      </tbody>
    </table>
  </div>
  <br>
  <div class="text-center">
    <div class="text-center pt-0">
      <button class="btn btn-success" type="submit" value="Save">Save</button>
      <button class="btn btn-danger" onclick="cancel()" value="Cancel">Cancel</button>
    </div>
  </div>
</form>
  • This is my Controller
public function create(Request $request)
{
    if ($request->get('servescol')) {
        $roles = DB::select('SELECT id, role_name FROM roles WHERE id <= 6');
        return view('user.create', compact('roles'));
    }
            
    if ($request->get('indicators')) {
        $roles = DB::select('SELECT id, role_name FROM roles WHERE id > 6');

        return view('user.create', compact('roles'));
    }
    
    $roles = Role::orderby('id')->get();
    $departments = Department::all(['id', 'department_name']);
    
    return view('user.create', compact('roles', 'departments'));
}

By the way, Departments is something different than "areas". The conditions <= 6 and > 6 correspond to the id according to the areas they belong to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏至、离别 2025-01-21 01:43:02

使用查询字符串,您可以制作类似的东西

<tbody>
  <tr>
    <td colspan="2">
      <select onchange="location.search = '?type=' + this.value">
        <option selected disabled></option>
        <option value="servescol">Servescol Roles</option>
        <option value="indicators">Indicators Roles</option>
      </select>      
    </td>
  </tr>
</tbody>
if ($request->query('type') === 'servescol') {
    $roles = DB::select('SELECT id, role_name FROM roles WHERE id <= 6');

    return view('user.create', compact('roles'));
}
        
if ($request->query('type') === 'indicators') {
    $roles = DB::select('SELECT id, role_name FROM roles WHERE id > 6');

    return view('user.create', compact('roles'));
}

Using the query string, You could make something like

<tbody>
  <tr>
    <td colspan="2">
      <select onchange="location.search = '?type=' + this.value">
        <option selected disabled></option>
        <option value="servescol">Servescol Roles</option>
        <option value="indicators">Indicators Roles</option>
      </select>      
    </td>
  </tr>
</tbody>
if ($request->query('type') === 'servescol') {
    $roles = DB::select('SELECT id, role_name FROM roles WHERE id <= 6');

    return view('user.create', compact('roles'));
}
        
if ($request->query('type') === 'indicators') {
    $roles = DB::select('SELECT id, role_name FROM roles WHERE id > 6');

    return view('user.create', compact('roles'));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文