1

i want add uploadFile to: https://github.com/thyseus/yii2-message

i added public $file2 in Message.php in model

and this is my rules in model:

    public function rules()
{
    return [
            [['title'], 'required'],
            [['file2'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
            [['title', 'message', 'context'], 'string'],
            [['title'], 'string', 'max' => 255],
            [['to'], IgnoreListValidator::class],
            [['to'], 'exist',
            'targetClass' => Yii::$app->getModule('message')->userModelClass,
            'targetAttribute' => 'id',
            'message' => Yii::t('app', 'Recipient has not been found'),
        ],
            [['to'], 'required', 'when' => function ($model)
            {
                return $model->status != Message::STATUS_SIGNATURE && $model->status != Message::STATUS_DRAFT;
            }],
    ];
}

and this is my view:

    <?= $form->field($model, 'message')->textarea(['rows' => 6]) ?>
    <?php
    echo $form->field($model, 'file2')->widget(FileInput::classname(), [
        'options' => ['accept' => 'image/*'],
    ]);

// With model & without ActiveForm
        echo '<label class="control-label">Add Attachments</label>';
        echo FileInput::widget([
            'model' => $model,
            'attribute' => 'file2',
            'options' => ['multiple' => true]
        ]);
        ?>

and this is my sendMessage Function

  $model = new Message();
    $model->attributes = $attributes;
    var_dump($attributes);
    die; 

why after upload file my result is this?

array(4) { ["to"]=> array(1) { [0]=> string(2) "65" } ["title"]=> string(3) "asd" ["message"]=> string(30) "asd  . " ["file2"]=> string(0) "" } 

file2 is empty !

Saltern
  • 1,305
  • 2
  • 16
  • 42
  • please add your complete action code this wont help to solve the problem you are not getting the `UploadedFile::instance()` anywhere you wont get the file inside the modal attributes array – Muhammad Omer Aslam Apr 25 '18 at 22:31

1 Answers1

1

When setting up the form, you need to mention as multipart/form-data. check below example.

<?php
use yii\widgets\ActiveForm;
?>

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

<?= $form->field($model, 'file2')->fileInput() ?>

<button>Submit</button>

<?php ActiveForm::end() ?>
Azraar Azward
  • 1,586
  • 2
  • 12
  • 16