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

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

Hello,

I'm developing an employee management system, where i have to upload employee documents such as passport, id, etc... may be more documents in future. So i can't make static input fields.
Every document contain 3 common things suchs as (doc_no, doc_expiry_date, doc_type_id, doc_file).
I have implemented the form and doing the validation but, How i can display the error message if validation fails for appropriate document ?

Here is my form :
PHP Code:
<?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="doc_no[]"
                                            type="text" value="" id="html5-text-input">
                                    </div>
                                    <input type="hidden" name="doc_type[]" value="<?= $doc['id'?>">
<span class="error"><?=session('errros.doc_no')?></span>


                                </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="doc_expiry[]" value=""
                                            id="html5-date-input">

                                    </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="file[]" multiple>
                                    </div>
                                </div>

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

                <?php endforeach; ?>
Here is my controller :

PHP Code:
public function documents()
    {
        $documents = new \App\Entities\DocumentsEntity;
        $doc_model = new DocumentsModel;


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

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

            $doc_files $this->request->getFileMultiple('file');
            $doc_no $this->request->getPost('doc_no');
            $doc_expiry $this->request->getPost('doc_expiry');
            $doc_type $this->request->getPost('doc_type');

            $data = [];

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

                if (!empty($doc_no[$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 (!empty($data)) {

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

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

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

                }

            } else {


                return redirect()->back()
                    ->with('warning''Nothing to update !');
            }

        }

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

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

        ]);
    


So this form will display 6 section based on database entries for document type. I need to show the error message for appropriate section


If one section have error message it is showing to all section. see image below

[Image: 2AHj0FQtSQyyrwthsjTFBg.png]
Reply


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



Theme © iAndrew 2016 - Forum software by © MyBB