空值正在验证中.拉拉韦尔 [英] The null value is being validated. Laravel

查看:49
本文介绍了空值正在验证中.拉拉韦尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有动态添加/删除输入的表单,但是,如果我添加输入但不输入任何内容,则将null值写入数据库,验证程序不会捕获null.Googling没有找到类似的问题,相反,每个人都对如何通过验证器传递null感兴趣.也许我对Google的搜索不满意.

I have a form with dynamically added / removed inputs, however if I add input and don't enter anything, the null value is written to the database, the validator does not catch null. Googling did not find a similar question, on the contrary, everyone was interested in how to pass null through the validator. Maybe I didn't google it well.

$request->validate([
    'properties' => 'required|min:1',
]);

属性:

<div class="input-group row">
                  <label for="category_id" class="col-sm-2 col-form-label">Product properties: </label>
                  <div class="row">
                      <div class="col-lg-12">
                          <div id="inputFormRow">
                            <div class="input-group mb-3">
                              @isset($product)
                                @foreach($product->properties as $prod)
                                    <input type="text" name="properties[][key]" value="{{ $prod['key'] ?? '' }}" class="form-control m-input editinp-key" placeholder="Key" autocomplete="off">
                                    <input type="text" name="properties[][value]" value="{{ $prod['value'] ?? '' }}" class="form-control m-input ml-3 editinp-value" placeholder="Value" autocomplete="off">
                                    <div class="input-group-append ml-3">
                                        <button id="removeRow" type="button" class="btn btn-danger">Remove</button>
                                    </div>
                                @endforeach
                              @endisset
                                @if(Session::has('properties'))
                                  @foreach(Session::get('properties') as $prop)
                                    <input type="text" name="properties[][key]" value="{{ $prop['key'] ?? '' }}" class="form-control m-input" placeholder="Key" autocomplete="off">
                                    <input type="text" name="properties[][value]" value="{{ $prop['value'] ?? '' }}" class="form-control m-input ml-3" placeholder="Value" autocomplete="off">
                                  <div class="input-group-append ml-3">
                                      <button id="removeRow" type="button" class="btn btn-danger">Remove</button>
                                  </div>
                                  @endforeach
                                @endif
                              </div>
                          </div>

                          <div id="newRow"></div>
                          <button id="addRow" type="button" class="btn btn-info">Add</button>
                      </div>
                  </div>
                </div>

jQuery:

$("#addRow").click(function () {
        var html = '';
        html += '<div id="inputFormRow">';
        html += '<div class="input-group mb-3">';
        html += '<input type="text" name="properties[][key]" class="form-control m-input ml-3" placeholder="Key" autocomplete="off">';
        html += '<input type="text" name="properties[][value]" class="form-control m-input ml-3" placeholder="Value" autocomplete="off">';
        html += '<div class="input-group-append ml-3">';
        html += '<button id="removeRow" type="button" class="btn btn-danger">Remove</button>';
        html += '</div>';
        html += '</div>';
        
        $('#newRow').append(html);
    });

  // remove row
  $('#removeRow').on('click', function () {
      $(this).closest('#inputFormRow').remove();
  });

推荐答案

假设您使用 key value 键传递对象数组,则可以像这个:

Assuming you pass an array of objects with key and value keys, you can validate your request like this:

$request->validate([
   'properties' => 'required|min:1',
   'properties.*.key' => 'required',
   'properties.*.value' => 'required',
]);

这篇关于空值正在验证中.拉拉韦尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆