OctoberCMS Responsiv Uploader Plugin, files has null values

90 views Asked by At

I use the uploader plugin to upload pdf files. I have followed the documentation implementing it on the components in the init function. this my following code

    public function init(){
    $tripFiles = $this->addComponent(
        'Responsiv\Uploader\Components\FileUploader',
        'tripFiles',
        [
            'fileTypes'             => '.jpg, .jpeg, .png, .pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx',
            'placeholderText'       => "Drag and drop file here or Browse File",
        ],
        ['deferredBinding'       => true]
    );
    $tripFiles->bindModel('files', ($this->property('tripId') ? Trip::find((int)$this->property('tripId')) : new Trip));
}


public function onSave(){
   $data = post();
    var_dump($data);
    $vacation = new Trip;
    $vacation->fill($data);
    $vacation->save(null, post('_session_key'));
}

but I found the output with null value on array of _uploader in files.

array:10 [▼
  "_handler" => "onCreateXXX"
  "_session_key" => "XXXXXXXXXX"
  "_token" => "XXXXXXXXXX"
  "name" => "XXXXX"
  "destination_name" => "XXXX"
  "purpose_id" => "1"
  "purpose" => ""
  "charge_code" => "XXXX"
  "city_id" => "1"
  "_uploader" => array:1 [▼
    "files" => ""
  ]
]

FrontEnd

{{ form_ajax('onSave')}}
   {% component 'tripFiles' %}
  <button type="submit" class="btn btn-primary w-90"> Create Trip </button>
{{ form_close() }}

so anyone can help me this problem ??

1

There are 1 answers

0
Hardik Satasiya On

Here problem is there are 2 models, and yes you wont be able to see file's data in post I guess.

  1. which is used in init either new one or based on tripId
  2. which is again new model in onSave anyhow not related to model which is binded to the widget in init method.

You need to make sure same model is used in both case so, may be create one variable which can hold model and use that in both scenario.

public $vacationModel = null;

public function init() {
    $tripFiles = $this->addComponent(
        'Responsiv\Uploader\Components\FileUploader',
        'tripFiles',
        [
            'fileTypes'             => '.jpg, .jpeg, .png, .pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx',
            'placeholderText'       => "Drag and drop file here or Browse File",
        ],
        ['deferredBinding'       => true]
    );
    $this->vacationModel = $this->property('tripId') 
          ? Trip::find((int)$this->property('tripId')) 
          : new Trip;
    $tripFiles->bindModel('files', $this->vacationModel);

}

public function onSave() {
    $data = post();
    $this->vacationModel->fill($data);
    $this->vacationModel->save(null, post('_session_key'));   
}

This should solve your issue.

if any doubts please comment.