'A way to exclude file upload based on condition
So I have to ask the user to upload their documents based on a condition, if their company type is A, they need to upload form A and a copy of their ID Card, then if their company type is B they need to upload form B and a copy of their ID. I have created a form which accepts 3 files, form A, B and ID card uploads. And have set up a jQuery to only show the file uploads for the specific files they need to upload.
The jQuery runs and hides the other file upload field when the user selects company type. But the form is unable to submit because of the validation in the model. How do i go about addressing this problem?
form.php
<?= $form->field($model, 'attachments[0]')->fileInput()->label('MyKad') ?>
<?= $form->field($model, 'attachments[1]')->fileInput()->label('Form 9 SSM (Limited Company)') ?>
<?= $form->field($model, 'attachments[2]')->fileInput()->label('Form D SSM (Sole Proprietor/Partnership)') ?>
jQuery
$("#i11").click(function() {
$(".form-group.row.field-custregistration-attachments-2").hide(); //Sole Prop
$(".form-group.row.field-custregistration-attachments-1").show(); //Limited company
$(".form-group.row.field-custregistration-attachments-2").removeClass("required"); //Limited company
}); //Limited company
$("#i12").click(function() {
$(".form-group.row.field-custregistration-attachments-2").show(); //Sole Prop
$(".form-group.row.field-custregistration-attachments-1").hide(); //Limited company
$(".form-group.row.field-custregistration-attachments-1").removeClass("required"); //Limited company
}); //sole prop
model validation
public function rules()
{
return [
[['dateOfApplication', 'requestType', 'portAccess', 'companyName', 'companyAddress', 'companyRegistrationNumber', 'companyContactNumber', 'companyType', 'customerFullName', 'customerEmail', 'customerICNo', 'customerContactNumber', 'status', 'attachments', 'uniqueID'], 'required'],
[['dateOfApplication','attachments'], 'safe'],
// [['uniqueID'], 'string'],
[['requestType', 'status'], 'string', 'max' => 50],
[['companyName', 'companyAddress', 'companyRegistrationNumber', 'companyContactNumber', 'companyType', 'customerEmail', 'customerICNo', 'customerContactNumber'], 'string', 'max' => 100],
[['customerFullName'], 'string', 'max' => 250],
[['debtorCode'], 'string', 'max' => 500],
[['attachments'],'file','extensions' => 'png,jpg,pdf', 'maxFiles' => 3],
// ['attachments', 'validateAttachments'],
// [['attachments'],'default', 'value' => 'N/A'],
];
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
