eniac05
eniac05

Reputation: 491

yii2 file extension validation not works

I have rule in my model like this

public function rules()
{
    return [
        [['tbl_data_induk_mahasiswa_id'], 'required'],
        [['tbl_data_induk_mahasiswa_id'], 'integer'],
        [['nama'], 'file','extensions'=>'png,jpg','maxSize' => 1024000,'tooBig' => 'Size maksimum adalah 1 MB'],

        [['nama'], 'string', 'max' => 300],

        [['tbl_data_induk_mahasiswa_id'], 'unique'],
        [['tbl_data_induk_mahasiswa_id'], 'exist', 'skipOnError' => true, 'targetClass' => TblDataIndukMahasiswa::className(), 'targetAttribute' => ['tbl_data_induk_mahasiswa_id' => 'id']],
    ];
}

i have form like this

<?php
use yii\helpers\Html;
//use yii\widgets\ActiveForm;
echo Html::beginForm(
     ['mahasiswa-foto-biodata/update'],
     'post',
     ['enctype' => 'multipart/form-data'] //if you want to upload file with post
); ?>
<div class="form-group form-file-upload form-file-multiple">
    <?= Html::activeFileInput(
        $model,
        'nama',
        ['class' => 'inputFileHidden', 'multiple' => '']
    ); ?>
    <div class="input-group">
        <?= Html::activeTextInput(
            $model,
            'nama', 
            [
                'class' => 'form-control inputFileVisible',
                'placeholder' => 'Single File',
            ]
        ); ?>
        <span class="input-group-btn">
            <button type="button" class="btn btn-fab btn-round btn-primary">
                <i class="material-icons">attach_file</i>
            </button>
        </span>
    </div>
</div> 
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>  
<?= Html::endForm(); ?>

I can not use activeForm widget because i must create html form that suit with my template, the form is work, the file succesfull uploaded, the problem is every tipe file is success uploaded, not just only png or jpg, but if i change the max rule for nama to [['nama'], 'string', 'max' => 2],then i upload a file that have name's length more than two the file can not be uploaded.

Any help?

Upvotes: 1

Views: 321

Answers (1)

namdt55555
namdt55555

Reputation: 469

Please check your form, you have 02 types of input with the same name ("nama"). One is "file", one is "text" I think it is the reason that make your form does not works correctly!

Upvotes: 1

Related Questions