Why the file with .csv does not pass validation in Yii2?

Validation rules:
public function rules()
{
 return [
 ['contact_file', 'file', 'skipOnEmpty' => false, 'extensions' => 'csv'],
];
 }

If you remove the 'extensions' => 'csv' that passes validation then why CSV does not pass the validation?
June 8th 19 at 17:21
2 answers
June 8th 19 at 17:23
['contact_file', 'file', 'skipOnEmpty' => false, 'extensions' => ['csv']],
in theory this does not matter in the proof specified string|array - violet_Dibbe commented on June 8th 19 at 17:26
June 8th 19 at 17:25
Form:
<?php $form = ActiveForm::begin(['method' =--> 'post', 'id' => 'add-contacts-file', 'options' => ['class' => 'mt-2 mb-2 col-lg-4 col-xs-3', 'enctype' => 'multipart/form-data'] ]); ?>

 <div class="custom-file">
 <?= $form--->field($addFileContact, 'contact_file')->fileInput(['class' => 'custom-file-input'])->label(Yii::t('app', 'Choose file'), ['class' => 'custom-file-label', 'for' => 'customFile']);
?>

 <div style="margin-top: -15px"><?= Yii::t('app', 'File type should be .csv') ?></div>

</div>

 <?= Html::submitButton(Yii::t('app', 'Add contacts'), ['class' =--> 'mt-2 btn btn-sm button-success', 'form' => 'add-contacts-file']) ?>

 <?php ActiveForm::end(); ?>


csv file

The povna model
/**
 * @var UploadedFile
*/
 public $contact_file;

 public function rules()
{
 return [
 ['contact_file', 'file', 'skipOnEmpty' => false, 'extensions' => 'csv'],
];
}

/**
 * @inheritdoc
*/
 public function attributeLabels()
{
 return [
 'contact_file' => 'Contact file',
];
}

 public function uploadContacts()
{
 if ($this->validate()) {
 $this->contact_file->saveAs('uploads/contacts/' . $this->contact_file->baseName . '.' . $this->contact_file->extension);
 return true;
 } else {
 return false;
}
 }
controller method can show? in the form all is well - violet_Dibbe commented on June 8th 19 at 17:28
controler
$addFileContact = new AddContactsFile();
 if (Yii::$app->request->isPost) {
 $addFileContact->contact_file = UploadedFile::getInstance($addFileContact, 'contact_file');
 if ($addFileContact->uploadContacts()) {
debug('success');
}
 }
- Mitchell_Zemlak16 commented on June 8th 19 at 17:31
fucking everything is fine, but try changing the extension on a picture (jpg) and upload your image in this format or not? ) - archibald.Jas commented on June 8th 19 at 17:34
tried a file with a png it works - Mitchell_Zemlak16 commented on June 8th 19 at 17:37
maybe you found the bug ) try to rename your csv file and remove the parenthesis just to name 1.csv - archibald.Jas commented on June 8th 19 at 17:40
not all one work, the problem in validation - Mitchell_Zemlak16 commented on June 8th 19 at 17:43
damn well can't be that )) can you have in a file name "a" is not English, and Russian or Vice versa in the validation - archibald.Jas commented on June 8th 19 at 17:46
changed all the same does not work, now try it on another project - Mitchell_Zemlak16 commented on June 8th 19 at 17:49
on another project, also not working, most likely this is a bug - archibald.Jas commented on June 8th 19 at 17:52
the csv file as receive. Generate? - archibald.Jas commented on June 8th 19 at 17:55
write in Đ•xel(2013) and save to csv - Dion77 commented on June 8th 19 at 17:58
Solved the problem? - archibald.Jas commented on June 8th 19 at 18:01
no work without validation, then we need to think of something - Mitchell_Zemlak16 commented on June 8th 19 at 18:04
I have found that validation of the file does not pass file .rtf, .css.
It seems to me that the validation did not miss files of type 'text/ * '.
Validation works fine on media files.
I decided to do the validation through js files:
var filesExt = ['jpg', 'gif', 'png']; // array of extensions
$('input[type=file]').change(function(){
 var parts = $(this).val().split('.');
 if(filesExt.join().search(parts[parts.length - 1]) != -1){
alert('Good!');
 } else {
alert('WTF?!');
}
});
- archibald.Jas commented on June 8th 19 at 18:07

Find more questions by tags Yii