M
M
Maxemp2018-03-09 12:46:43
Yii
Maxemp, 2018-03-09 12:46:43

Why is the upload button not working?

The problem is that after selecting an image and pressing the "upload" button, nothing happens at all, and even when I select the second image, the one that I selected first disappears and is replaced by the second one, although this should not happen.
Since I don’t know what it could be, I’ll leave all the code that relates to uploading pictures of the
Photo Entity:

class Photo extends ActiveRecord
{

  public static function create(UploadedFile $file):self
  {
    $photo = new static();
    $photo->file = $file;
    return $photo;
  }

  public static function tableName()
  {
    return '{{%photo}}';
  }

  public function behaviors() {
    return [
      [
        'class' => ImageUploadBehavior::className(),
        'attribute' => 'file',
        'createThumbsOnRequest' => true,
        'filePath' => '@staticRoot/origin/.',
        'fileUrl' => '@static/origin/.',
        'thumbPath' => '@staticRoot/cache/_.',
        'thumbUrl' => '@static/cache/_.',
        'thumbs' => [
          'admin' => ['width' => 100, 'height' => 70],
          'thumb' => ['width' => 640, 'height' => 480]
        ],
      ],
    ];
  }

}

controller:
class PhotoController extends Controller
{
  private $service;

  public function __construct( $id, $module, array $config = [], PhotoManageService $service )
  {
    $this->service = $service;
    parent::__construct( $id, $module, $config );
  }

  public function actionCreate()
  {
    $form = new PhotoForm();
    if ($form->load(Yii::$app->request->post()) && $form->validate()){
      try{
        $this->service->addPhotos($form);
        Yii::$app->session->setFlash('success', 'photo uploaded');
        return $this->goHome();
      }catch(\RuntimeException $e){
        Yii::$app->errorHandler->logException($e);
        Yii::$app->session->setFlash($e->getMessage());
      }
    }

    return $this->render('create', ['model' => $form]);

  }

}

service:
class PhotoManageService
{
  private $photos;

  public function __construct( PhotoRepository $photos )
  {
    $this->photos = $photos;
  }

  public function addPhotos(PhotoForm $form):void
  {
    foreach($form->files as $file){
      $photo = Photo::create($file);
      $this->photos->save($photo);
    }
  }

}

performance:
<div class="box box-default">
  <?php $form = ActiveForm::begin([
        'enableClientValidation' => false,
    'options' => ['enctype'=>'multipart/form-data']
  ]); ?>
  <div class="box-body">
    <?= $form->field($model, 'files[]')->widget(FileInput::class, [
      'options' => [
        'accept' => 'image/*',
        'multiple' => true,
      ]
    ])
    ?>
  </div>

  <? ActiveForm::end();?>
</div>

The form:
class PhotoForm extends Model
{

  /**
   * @var UploadedFile[]
   */
  public $files;

  public function rules() {
    return [
      ['files', 'each', 'rule' => ['image']],
    ];
  }

  public function beforeValidate(): bool
  {
    if (parent::beforeValidate()){
      $this->files = UploadedFile::getInstances($this, 'files');
      return true;
    }
    return false;
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question