Search code examples
phpjquerylaravelsweetalert

how to passing validation laravel controller to sweet alert swal


I first tried sweet alert here. I want to create a data input eror validation list displayed in sweet alert, first I tried not to fill all textfields so the result would be a list of anything that misinput or not filling in on textfield and being sampled in sweet alert. I've tried but only get last his value is just a list of unbending data, please help me

controller

 public function simpanpegawai(Request $req)
    {
        $messages = [
            'nama.required' => 'Field Nama Gak Boleh Kosong',
            'nama.min' => 'Field nama tidak boleh kurang dari 5 kata',
            'nama.regex' => 'Field nama tidak boleh angka',
            'alamat.required' => 'Field Alamat Gak Boleh Kosong',
            'alamat.min' => 'Field alamat tidak boleh 5 kata',
            'tempat_lahir.required' => 'Field tempat lahir harus diisi !',
            'tempat_lahir.regex' => 'field Tempat lahir tidak boleh ada angka',
            'tgl_lahir.required' => 'Field Tanggal Lahir harus diisi !',
            'tgl_lahir.date' => 'Field Tanggal Lahir harus format tanggal!',
            'pendidikan_terkahir.not_in' => 'Field pendidikan terkahir Gak Boleh Kosong',
            'no_telp.required' => 'Field no telpon Gak Boleh Kosong',
            'no_telp.min' => 'Field no telpon Harus minimal 10 ',
            'jkel.not_in' => 'Field jenis kelamin harus diisi !',
            'id_jabatan.not_in' => 'Field jabatan harus diisi !',
            'status.not_in' => 'Field status harus diisi !',
            ];

        $validator = \Validator::make($req->all(), [
            'nama' => 'required|min:5|regex:/^[a-zA-Z]+(([\',. -][a-zA-Z ])?[a-zA-Z]*)*$/',
            'alamat' => 'required|min:5',
            'no_telp' => 'required|min:10',
            'tempat_lahir' => 'required|min:5|regex:/^[a-zA-Z]+(([\',. -][a-zA-Z ])?[a-zA-Z]*)*$/',
            'tgl_lahir' => 'required|date',
            'pendidikan_terkahir' => 'not_in:0',
            'jkel' => 'not_in:0',
            'id_jabatan' => 'not_in:0',
            'status' => 'not_in:0',
        ], $messages);
        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()->all()]);
        } else {
            $user = new \App\User;
            $user->name = $req->nama;
            $user->role = $req->id_jabatan;
            $user->username = $req->nama;
            $user->password = bcrypt($req->no_telp);
            $simpan = $user->save();


            $req->request->add(['user_id' => $user->id]);
            $pegawai = \App\PegawaiModel::create($req->all());
            if ($simpan == 1) {

                $status = "Tersimpan";

            } else {
                $status = "Gagal";
            }
            echo json_encode(array("status" => $status));
        }
    }

sweet alert in blade script

$.ajax({
                url: url,
                type: "POST",
                data: $('#formpegawai').serialize(),
                dataType: "JSON",
                success: function (data) {
                    if (data.errors) {

                        jQuery.each(data.errors, function (key, value) {
                            swal({
                                title: "Pesan Eror",
                                text: value,
                                timer: 5000,
                                showConfirmButton: false,
                                type: "error"
                            })
                        });

                    } else {
                        swal({
                            text: data.status,
                            timer: 5000,
                            icon:"success",
                            showConfirmButton: false,
                            type: "error"
                        })
                        $('#modal_form').modal('hide');
                    }
                },
                error: function (request, status, error) {

                }
            });

picture enter image description here


Solution

  • Basically because your sweetalert is inside in your foor each, now the value is overwrite everytime the loop increments. To solve this please move your sweet alert after your loop

    if(data.errors) {
        var values = '';
        jQuery.each(data.errors, function (key, value) {
             values += value
        });
    
        swal({
            title: "Pesan Eror",
            text: values,
            timer: 5000,
            showConfirmButton: false,
            type: "error"
        })
    }