FriendsOfCake/cakephp-upload

Plugin not working on cake 3.8

ZakkProjects opened this issue · 2 comments

Hi i´ve installed the plugin with composer a added $this->addPlugin('Josegonzalez/Upload'); to model\application.php.

but after i´ve send the form file is not saved and no data are i database.

gallery/add.ctp

<div class="gallery form large-9 medium-8 columns content">
    <?= $this->Form->create($gallery) ?>
    <fieldset>
        <legend><?= __('Add Gallery') ?></legend>
        <?php
            echo $this->Form->control('title');
            echo $this->Form->control('img_url');
            echo $this->Form->control('img_alt');
            echo $this->Form->input('photo', ['type' => 'file']);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

Gallery controller:

public function add()
    {
        $gallery = $this->Gallery->newEntity();
        if ($this->request->is('post')) {
            $gallery = $this->Gallery->patchEntity($gallery, $this->request->getData());
            if ($this->Gallery->save($gallery)) {
                $this->Flash->success(__('The gallery has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The gallery could not be saved. Please, try again.'));
        }
        $this->set(compact('gallery'));
    }

Gallery table

public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('gallery');
        $this->setDisplayField('title');
        $this->setPrimaryKey('id');

        $this->addBehavior('Josegonzalez/Upload.Upload', [
            'photo',
        ]);
    }
public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmptyString('id', null, 'create');

        $validator
            ->scalar('title')
            ->maxLength('title', 50)
            ->requirePresence('title', 'create')
            ->notEmptyString('title');

        $validator
            ->scalar('img_url')
            ->maxLength('img_url', 50)
            ->requirePresence('img_url', 'create')
            ->notEmptyString('img_url');

        $validator
            ->scalar('img_alt')
            ->maxLength('img_alt', 50)
            ->requirePresence('img_alt', 'create')
            ->notEmptyString('img_alt');
        
        $validator
            ->scalar('photo')
            ->maxLength('photo', 255)
            ->requirePresence('photo', 'create')
            ->notEmptyString('photo');

        return $validator;
    }

ok now i´ve got it to write the filename to database, but the file is not saving to webroot

Your form type is not file.

<?= $this->Form->create($gallery, ['type' = 'file') ?>

Check the examples https://github.com/FriendsOfCake/cakephp-upload/blob/master/docs/examples.rst