Yii2 DynamicForm输入文件图像始终为空或NULL

时间:2016-01-21 02:45:02

标签: php yii yii2 yii-extensions

我正在使用wbraganca yii2-dynamicform和kartik yii2-widget-fileinput。表单工作正常,但是当我尝试上传的getinstance时,图像总是为空。

这是我的控制器。

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;
            }
        }

查看表单

  //_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' => '']
                        ]
                    ]) ?>

和我的模特

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']
    ];
}

yii2 log的截图。更新

yiilog

检查元素截图 html

感谢!!!

1 个答案:

答案 0 :(得分:1)

'enctype' => 'multipart/form-data'中添加ActiveForm

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

如果您不想进行Ajax验证,请将Ajax验证设置为false。

Reference