Hello,
I have a simple dynamic form with 4 inputs including one hidden input. here i use validation rule for array data because we can't predict how many fields will be added in future.
I use required validation for all these fields. but everything work expect the file.
My Form :
Code:
<?= form_open_multipart('dashboard/employees/documents') ?>
<div class="row">
<?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[]" type="text"
value="" id="html5-text-input">
<input type="hidden" name="doc_type[]" value="<?= $doc['id'] ?>">
</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[]" value=""
id="html5-date-input">
<span class="error">
</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[]">
<span class="error">
</span>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
Controller :
Code:
public function documents()
{
$documents = new \App\Entities\DocumentsEntity;
$doc_model = new DocumentsModel;
$validation = \Config\Services::validation();
if ($this->request->getPost()) {
$rules = array();
$count = count($doc_no = $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[]');
for ($i = 0; $i <= $count; $i++) {
$rules += array(
'no.' . $i . '' => 'required',
'expiry_date.' . $i . '' => 'required',
'doc_file.' . $i . '' => 'required',
);
}
if (!$this->validate($rules)) {
echo '<pre>';
print_r($this->validator->getErrors());
echo '<pre>';
exit;
}
}
$doc_types = new DocumentTypesModel;
$doc_types = $doc_types->findAll();
return view('employees/documents', [
'doc_types' => $doc_types,
]);
}
How to solve this?