首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果存在验证错误,请保持模态打开。

如果存在验证错误,请保持模态打开。
EN

Stack Overflow用户
提问于 2022-06-07 08:19:07
回答 1查看 97关注 0票数 1

我试图上传一个csv,并相应地将数据存储在数据库中。表格在模态中。如果存在任何验证错误消息,我希望打开该模式。

下面是模式和ajax查询:

代码语言:javascript
复制
    $('#formSubmit').click(function(e) {
    e.preventDefault();
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('input[name="_token"]').val()
        }
    });
    $.ajax({
        url: "{{ url('/resellers') }}",
        type: 'POST',
        contentType: false,
        processData: false,
        data: {
            csv_file: $('#csv_file').val(),
        },
        success: function(result) {
            if (result.errors) {
                $('.alert-danger').html('');
                $.each(result.errors, function(key, value) {
                    $('.alert-danger').show();
                    $('.alert-danger').append('<li>' + value + '</li>');
                });
            } else {
                $('.alert-danger').hide();
                $('#reseller_modal').modal('hide');
            }
        }
    });
});


        <div class="col-sm-6">
        <button type="button" id="csv-import" class="btn btn-secondary m-2 csv-import" data- toggle="modal" data-target="#reseller_modal">Import Csv</button>
    </div>
    <!-- Bootstrap modal -->
    <div class="modal fade" id="reseller_modal" tabindex="-1" role="dialog" aria- labelledby="reseller_modal" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="demoModalLabel">CSV import</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="alert alert-danger" style="display:none"></div>
                <div class="modal-body">
                    <div class="row">
                        <form class="form-horizontal" method="POST" action="{{ route('processImport') }}" enctype="multipart/form-data">
                            {{ csrf_field() }}
                            <div class="form-group">
                                <input id="csv_file" type="file" class="form-control" name="csv_file" required>
                                @if ($errors->has('csv_file'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('csv_file') }}</strong>
                                </span>
                                @endif
                            </div>
                            <div class="form-check">
                            </div>
                            <button type="submit" class="btn btn-primary" id="formSubmit">Submit</button>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

这里是控制器:

代码语言:javascript
复制
 public function processImport(Request $request)
{
    $validator = Validator::make($request->all(), [
        'csv_file' => 'required|file|mimes:csv,txt'
    ]);
    if ($validator->fails()) {
        return response()->json(['errors' => $validator->errors()->all()]);
    }
    $rows = importCsv($request->file('csv_file'));
    foreach ($rows as $data) {
        $validator = Validator::make($data, [
            'name' => ['required', 'string'],
            'email' => ['required', 'string', 'email'],
            'password' => ['required', 'string'],
        ]);
        $status = User::where('email', '=', $data['email'])->exists();
        if (!$validator->fails()) {
            if (!$status) {
                User::create([
                    'name' => $data['name'],
                    'email' => $data['email'],
                    'password' => bcrypt($data['password'])
                ]);
            } else {
                $user = User::where('email', '=', $data['email'])->first();
                $user->update([
                    'password' => $data['password']
                ]);
            }
        } else {
           Log::channel('import_fail')->info($data['email'] . ' Couldnot be stored . Has the validation message" : ' .  $validator->errors()->first());
        }
    }
    return redirect()->route('resellers')->with('success', 'Resellers imported successfully');

即使我有形式的csv_field,我得到了这个错误The csv_file field is required.,它保持了模态的打开。

EN

回答 1

Stack Overflow用户

发布于 2022-06-07 10:23:01

我想问题就在这里。

代码语言:javascript
复制
data: {
            csv_file: $('#csv_file').val(),
      },

您正在发送文本值,即文件名,而不是实际文件。和验证文件。

将此引用到使用ajax上传文件。https://makitweb.com/how-to-upload-a-file-using-jquery-ajax-in-laravel-8/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72528019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档