Archagy
Archagy

Reputation: 135

Yii2 DynamicForm Input File Image always empty or NULL

I'm using wbraganca yii2-dynamicform and kartik yii2-widget-fileinput. The form works fine but when I'm trying the getinstance for upload the images always is null.

This is my controller.

public function addMultipleImage($model){
       $modelsOptionValue = Model::createMultiple(OptionValue::classname());
        Model::loadMultiple($modelsOptionValue, Yii::$app->request->post());
        foreach ($modelsOptionValue as $index => $modelOptionValue) {
            $modelOptionValue->sort_order = $index;
            $file[$index]=  UploadedFile::getInstanceByName("OptionValue[".$index."][upload_image]");
            var_dump($file[$index]);
          if($file[$index]){
                $ext = end((explode(".", $file[$index]->name)));
                // generate a unique file name
                $modelOptionValue->img= Yii::$app->security->generateRandomString().".{$ext}";
                $path[$index]= Yii::getAlias ('@web') ."/img/". $modelOptionValue->img;
            }else{
                return false;
            }
        }

View form

  //_form.php
  <?php $form = ActiveForm::begin( [
    'enableClientValidation' => false,
    'enableAjaxValidation' => true,
    'validateOnChange' => true,
    'validateOnBlur' => false,
    'options' => [
        'enctype' => 'multipart/form-data',
        'id' => 'dynamic-form'
    ] 
    ]);
?>
  //_form_add_image.php
 <?php DynamicFormWidget::begin([
    'widgetContainer' => 'dynamicform_wrapper',
    'widgetBody' => '.form-options-body',
    'widgetItem' => '.form-options-item',
    'min' => 1,
    'insertButton' => '.add-item',
    'deleteButton' => '.delete-item',
    'model' => $modelsOptionValue[0],
    'formId' => 'dynamic-form',
    'formFields' => [
        'upload_image'
    ],
]); ?>

 <?= $form->field($modelOptionValue, "[{$index}]upload_image")->label(false)->widget(FileInput::classname(), [
                        'options' => [
                            'multiple' => false,
                            'accept' => 'image/*',
                            'class' => 'optionvalue-img'
                        ],
                        'pluginOptions' => [
                            'previewFileType' => 'image',
                            'showCaption' => false,
                            'showUpload' => false,
                            'browseClass' => 'btn btn-default btn-sm',
                            'browseLabel' => ' Seleccionar Imagen',
                            'browseIcon' => '<i class="glyphicon glyphicon-picture"></i>',
                            'removeClass' => 'btn btn-danger btn-sm',
                            'removeLabel' => ' Borrar',
                            'removeIcon' => '<i class="fa fa-trash"></i>',
                            'previewSettings' => [
                                'image' => ['width' => '138px', 'height' => 'auto']
                            ],
                            'initialPreview' => $initialPreview,
                            'layoutTemplates' => ['footer' => '']
                        ]
                    ]) ?>

and my model

public $upload_image;
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'option_value';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['upload_image'], 'file', 'extensions' => 'png, jpg', 'skipOnEmpty' => true],
        [['id_montacarga', 'sort_order'], 'integer']
    ];
}

screenshot of yii2 log. updated

yiilog

inspect element screenshot html

Thanks!!!

Upvotes: 1

Views: 2200

Answers (1)

Insane Skull
Insane Skull

Reputation: 9368

Add 'enctype' => 'multipart/form-data' in ActiveForm.

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

Set Ajax Validation to false if you don't want ajax validation.

Reference

Upvotes: 1

Related Questions