-
sknair143 unrelaxed !
 
-
Posts: 35
Threads: 12
Joined: Aug 2023
Reputation:
0
09-06-2023, 03:27 AM
(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
-
sknair143 unrelaxed !
 
-
Posts: 35
Threads: 12
Joined: Aug 2023
Reputation:
0
09-06-2023, 05:38 AM
(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>
-
sammyskills Member
  
-
Posts: 116
Threads: 2
Joined: Nov 2020
Reputation:
7
With the code you have, if you print_r or var_dump the errors session variable, you should able to get the "index" associated with the error message. So, your error message display should look something like this:
PHP Code: <?= session('errors.doc_no.0') ?>
Where the 0 indicates the index.
-
sknair143 unrelaxed !
 
-
Posts: 35
Threads: 12
Joined: Aug 2023
Reputation:
0
(09-06-2023, 05:38 AM)sammyskills Wrote: With the code you have, if you print_r or var_dump the errors session variable, you should able to get the "index" associated with the error message. So, your error message display should look something like this:
PHP Code: <?= session('errors.doc_no.0') ?>
Where the 0 indicates the index.
Yes, now im able to show error messages. but my validation is not working. it always showing the error message if i enter the data also.
-
sammyskills Member
  
-
Posts: 116
Threads: 2
Joined: Nov 2020
Reputation:
7
1. Are you validating in both the model and controller?
2. Within your controller, you do not need to load the validation library again. It is already available in controllers using $this->validate().
-
sknair143 unrelaxed !
 
-
Posts: 35
Threads: 12
Joined: Aug 2023
Reputation:
0
09-06-2023, 05:50 AM
(This post was last modified: 09-06-2023, 05:50 AM by sknair143.)
(09-06-2023, 05:46 AM)sammyskills Wrote: 1. Are you validating in both the model and controller?
2. Within your controller, you do not need to load the validation library again. It is already available in controllers using $this->validate().
No, i'm doing in my controller only because, i have multiple error messages for different document types.
(09-06-2023, 05:50 AM)sknair143 Wrote: (09-06-2023, 05:46 AM)sammyskills Wrote: 1. Are you validating in both the model and controller?
2. Within your controller, you do not need to load the validation library again. It is already available in controllers using $this->validate().
No, i'm doing in my controller only because, i have multiple error messages for different document types.
How i can workout this logic ?, Because in futre may be n number of document types will be added, so i can't put static fields
-
sammyskills Member
  
-
Posts: 116
Threads: 2
Joined: Nov 2020
Reputation:
7
|