I want to create file validation which not mandatory but when it is inserted, it has to be zip file and max_size 2MB. I use Codeigniter 4 & Js, With the code below, when the input[file] is empty, I still get validation error.
$("#btnReply").click(function(e) {
var frmReply = new FormData();
frmReply.append('attachment_reply', $('#attachment_reply')[0].files[0]);
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://localhost/syst20/ticketreply/store',
enctype: 'multipart/form-data',
dataType: 'json',
data: frmReply,
});
My Controller Rule
if (!$this->validate([
'attachment_reply' => [
'label' => 'Attachment',
'rules' => 'ext_in[attachment_reply,zip,rar,7z]|max_size[attachment_reply,2000]',
]])) {
$error_description = $this->validator->getErrors();
$callback = array(
'status' => 'failed',
'message' => $error_description,
'ticket_id' => $ticket_id,
'token' => csrf_hash(),
);
}