Welcome Guest, Not a member yet? Register   Sign In
Handling Multiple input with same field name as array. and show valdiation error
#3

(This post was last modified: 09-06-2023, 05:39 AM by sknair143.)

Check this :

Code:
        $documents = new \App\Entities\DocumentsEntity;
        $doc_model = new DocumentsModel;
        $validation = \Config\Services::validation();


        if ($this->request->getPost()) {

            $count = count($this->request->getPost('no')) - 1;

            $doc_files = $this->request->getFileMultiple('doc_file');
            $doc_no = $this->request->getPost('no[]');
            $doc_expiry = $this->request->getPost('expiry_date[]');
            $doc_type = $this->request->getPost('doc_type[]');

            foreach ($doc_no as $ind => $val) {
                $validation->setRule('no[' . $ind . ']', 'Doc No', 'required');
                $validation->setRule('expiry_date[' . $ind . ']', 'Expiry date', 'required');
                $validation->setRule('doc_file[' . $ind . ']', 'Doc File', 'required');

            }

            $data = [];

            for ($i = 0; $i <= $count; $i++) {

                $data_record =

                    [
                        'no' => $doc_no[$i],
                        'expiry_date' => $doc_expiry[$i],
                        'doc_type_id' => $doc_type[$i],
                        'doc_file' => $doc_files[$i]->getName()

                    ];


                array_push($data, $data_record);






            }


            if (!$validation->run($data)) {


                return redirect()->back()->with('warning', 'Errors')->with('errors', $validation->getErrors());

            } else {


                if ($doc_model->insertBatch($data) == TRUE) {

                    return redirect()->back()->with('warning', 'Success');
                } else {

                    return redirect()->back()
                        ->with('warning', 'Error')
                        ->with('errors', $doc_model->errors());

                }

            }

        }

        $doc_types = new DocumentTypesModel;
        $doc_types = $doc_types->findAll();

        return view('employees/documents', [
            'doc_types' => $doc_types,

        ]);
    }


Form 


Code:
<?= form_open_multipart('dashboard/employees/documents') ?>
            <div class="row">
                <?php $i = 0; ?>
                <?php foreach ($doc_types as $doc): ?>

                    <div class="mb-3 col-md-6">
                        <div class="card">
                            <h5 class="card-header">
                                <?= $doc['name'] ?>
                            </h5>
                            <div class="card-body">
                                <div class="mb-3 row">
                                    <label for="" class="col-md-2 col-form-label">Doc No</label>
                                    <div class="col-md-10">
                                        <input class="form-control" placeholder="Enter Valid Doc No" name="no[<?= $i ?>]"
                                            type="text" value="" id="html5-text-input">

                                        <input type="hidden" name="doc_type[]" value="<?= $doc['id'] ?>">
                                        <span class="error">
                                            <?= session('errors.no[' . $i . ']') ?>
                                        </span>
                                    </div>
                                </div>

                                <div class="mb-3 row">
                                    <label for="html5-text-input" class="col-md-2 col-form-label">Expiry Date</label>
                                    <div class="col-md-10">
                                        <input class="form-control" type="date" name="expiry_date[<?= $i ?>]" value=""
                                            id="html5-date-input">
                                        <span class="error">
                                            <?= session('errors.expiry_date[' . $i . ']') ?>
                                        </span>
                                    </div>
                                </div>

                                <div class="mb-3 row">
                                    <label for="html5-text-input" class="col-md-2 col-form-label">File</label>
                                    <div class="col-md-10">
                                        <input class="form-control" type="file" id="formFile" name="doc_file[<?= $i ?>]"
                                            multiple>
                                        <span class="error">
                                            <?= session('errors.doc_file[' . $i . ']') ?>
                                        </span>
                                    </div>

                                </div>

                            </div>
                        </div>
                    </div>

                    <?php $i++; endforeach; ?>




            </div>

        </div>
        <button type="submit" class="btn btn-primary">Save</button>
        </form>
Reply


Messages In This Thread
RE: Handling Multiple input with same field name as array. and show valdiation error - by sknair143 - 09-06-2023, 05:38 AM



Theme © iAndrew 2016 - Forum software by © MyBB