Jump to content

Set default value in renderForm() file input.


squni
 Share

Recommended Posts

Hi, 

I have quite a problem here. So I'm developing a module and one of it's features is to create a project which contains a file (image).

While creating it and saving this image is working fine, when you try to edit it then the problem appears. If you edit some fields and leave the file input untouched Prestashop overrides file with empty field.

I already checked that inside fields_value the files name is correct and I can't find the moment where it's getting overridden. Also tried using HelperForm() and setting $this->tpl_vars or $this->object->file. Nothing worked, everytime form gets submitted with file input empty unless you submit anothe file.

 

Here is form rendering function:

    public function renderForm()
    {

        $this->fields_form = array(
            'legend' => array(
                'title' => $this->trans('New project', array(), 'Admin.Catalog.Feature'),
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'label' => $this->trans('Name', array(), 'Admin.Global'),
                    'name' => 'name',
                    'required' => true,
                    'col' => 3,
                ),
                array(
                    'type' => 'select',
                    'options' => array(
                        'query' => Project::getGroups(),
                        'id' => 'id_group',
                        'name' => 'name',
                        ),
                    'label' => $this->trans('Group', array(), 'Admin.Global'),
                    'name' => 'id_group',
                    'required' => true,
                    'col' => 2,
                ),
                array(
                    'type' => 'select',
                    'options' => array(
                        'query' => Project::getProducts((int)Context::getContext()->language->id),
                        'id' => 'id_product',
                        'name' => 'name',
                        ),
                    'label' => $this->trans('Product', array(), 'Admin.Global'),
                    'name' => 'id_product',
                    'class' => 'js-example-basic-single',
                    'required' => true,
                    'col' => 3,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->trans('Height', array(), 'Admin.Global'),
                    'name' => 'height',
                    'required' => true,
                    'col' => 1,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->trans('Width', array(), 'Admin.Global'),
                    'name' => 'width',
                    'required' => true,
                    'col' => 1,
                ),
                array(
                    'type' => 'file',
                    'label' => $this->trans('File', array(), 'Admin.Global'),
                    'name' => 'file',
                    'required' => false,
                    'col' => 6,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->trans('CSS Class', array(), 'Admin.Global'),
                    'name' => 'class_name',
                    'required' => false,
                    'col' => 3,
                ),
                
            ),
            'submit' => array(
                'title' => $this->trans('Save', array(), 'Admin.Actions'),
                'name' => 'save_form',
            ),
        );

        return parent::renderForm();
    }

 

Here is file uploading function:

public function postProcess()
    {
        parent::postProcess();
        
        if (Tools::getIsset('save_form')) {

            // Uploads project image file

            if ($_FILES['file']['name'] != '') {
                $project = new Project(Project::getNewestProjectId());
                $project->file = $project->uploadFile($_FILES['file'], $project->id_project);
                $project->save();
            }

            $this->redirect_after = Context::getContext()->link->
            getAdminLink('AdminPersonalizationDesign') . '&id_project=' .Project::getCurrentProjectId();
        }
    }
}

public function uploadFile($file, $id_project)
    {
        $fileName = explode(".", $file["name"]);
        $ext = end($fileName);
        $name = uniqid(reset($fileName)).".".$ext;
        $id_folder = (int)ceil($id_project/1000);
        if (!file_exists(_PS_IMG_DIR_ ."pp/projects/".$id_folder)) {
            mkdir(_PS_IMG_DIR_ ."pp/projects/".$id_folder);
        }
        $location = _PS_IMG_DIR_ ."pp/projects/".$id_folder."/". $name;
        move_uploaded_file($file["tmp_name"], $location);
        return $name;
    }

 

Edited by squni
mistakenly submitted (see edit history)

Share this post


Link to post
Share on other sites

  • 3 months later...

RESOLVED

 

If anyone has the same problem, here's how I managed to deal with it.

In my controller responsible of generating the form I added those lines

        if (Tools::getValue('id_project')) {
            $project = new Project(Tools::getValue('id_project'));
            $file = array(
                    'type' => 'file',
                    'label' => $this->trans('File', array(), 'Admin.Global'),
                    'name' => $project->file,
                    'required' => false,
                    'col' => 6,
            );
        } else {
            $file = array(
                'type' => 'file',
                'label' => $this->trans('File', array(), 'Admin.Global'),
                'name' => 'file',
                'required' => false,
                'col' => 6,
            );
        }
        array_push($this->fields_form['input'], $file);

 

INSIDE MY CODE

 

    public function renderForm()
    {

        
        $this->fields_form = array(
            'legend' => array(
                'title' => $this->trans('New project', array(), 'Admin.Catalog.Feature'),
            ),
            'input' => array(
                array(
                    'type' => 'text',
                    'label' => $this->trans('Name', array(), 'Admin.Global'),
                    'name' => 'name',
                    'required' => true,
                    'col' => 3,
                ),
                array(
                    'type' => 'select',
                    'options' => array(
                        'query' => Project::getGroups(),
                        'id' => 'id_group',
                        'name' => 'name',
                    ),
                    'label' => $this->trans('Group', array(), 'Admin.Global'),
                    'name' => 'id_group',
                    'required' => true,
                    'col' => 2,
                ),
                array(
                    'type' => 'select',
                    'options' => array(
                        'query' => Project::getProducts((int)Context::getContext()->language->id),
                        'id' => 'id_product',
                        'name' => 'name',
                    ),
                    'label' => $this->trans('Product', array(), 'Admin.Global'),
                    'name' => 'id_product',
                    'required' => true,
                    'col' => 3,
                ),
                array(
                    'type' => 'select',
                    'options' => array(
                        'query' => array(
                            array('key' => 'print', 'name' => 'PRINT'),
                            array('key' => 'engrave', 'name' => 'ENGRAVE'),
                        ),
                        'id' => 'key',
                        'name' => 'name'
                    ),
                    'label' => $this->trans('Type', array(), 'Admin.Global'),
                    'name' => 'type',
                    'required' => true,
                    'col' => 3,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->trans('Height', array(), 'Admin.Global'),
                    'name' => 'height',
                    'required' => true,
                    'col' => 1,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->trans('Width', array(), 'Admin.Global'),
                    'name' => 'width',
                    'required' => true,
                    'col' => 1,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->trans('CSS Class', array(), 'Admin.Global'),
                    'name' => 'class_name',
                    'required' => false,
                    'col' => 3,
                ),
            ),
            'submit' => array(
                'title' => $this->trans('Save', array(), 'Admin.Actions'),
                'name' => 'save_form',
            ),
        );

################ HERE IS THE SOLUTION ################

        //prevent empty file override during project edit
        if (Tools::getValue('id_project')) {
            $project = new Project(Tools::getValue('id_project'));
            $file = array(
                    'type' => 'file',
                    'label' => $this->trans('File', array(), 'Admin.Global'),
                    'name' => $project->file,
                    'required' => false,
                    'col' => 6,
            );
        } else {
            $file = array(
                'type' => 'file',
                'label' => $this->trans('File', array(), 'Admin.Global'),
                'name' => 'file',
                'required' => false,
                'col' => 6,
            );
        }
        array_push($this->fields_form['input'], $file);

################ END OF THE SOLUTION ################

        return parent::renderForm();
    }

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More