Jump to content

Problems getting the selected values in a input select


Monika

Recommended Posts

Hi,

 

I'm trying to get the value from an input select form using this code:

 

$this->fields_form = [
            'legend' => ['title' => $this->l('Por favor ingresar los datos solicitados. Bienvenid@')
                ],
            'input'  => 'id_zona'   => ['type' => 'select', 'label'  => $this->l('Zona'),'name' => 'id_zona', 'required' => true, 'lang' => true, 'options'=>array('query'=>$zonas, 'id' =>'ID_ZONA', 'name' => 'CODIGOZONA')],
                         'id_tienda'   => ['type' => 'select', 'label'  => $this->l('Campaña'),'name' => 'id_tienda', 'required' => true, 'lang' => true, 'options'=>array('query'=>$campana, 'id' => 'id_campana', 'name' => 'campana', 'value'=> $this->fields_value['id_tienda']='id_campana')],
                         'id_empleado'   => ['type' => 'text', 'label'  => $this->l('Gerente de Zona'),'name' => 'id_empleado',  'lang' => true, 'value'=>$gerente, 'placeholder'=>$completename, 'disabled'=> true],
                        ],
            'submit' => ['title' => $this->l('Save')],
          
        ];

When the user select the value, the select inputs values selected are empty. How can I get the values selected. Thanks !!!

  • Like 1
Link to comment
Share on other sites

So, the "problem" is that you don't know how to retrieve the form's values?

A quick look in the PS controllers or any module that uses forms controllers would have revealed that to retrieve those variables you need to use

Tools::getValue('name')

In your controller form processing function.

EDIT: of course replace 'name' with your variable name

Edited by Rho_Bur (see edit history)
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

 

Hi Ambassadors,

Thanks for your response. I did my module using the prestashop core, I maked this form to be use in the bakcoffice of Prestashop 1.7.  I didn't using a TPL, I maked the form writing an array in the moduleAdminController class, into de __construct() method. Well, when I was using a type select field, the form didn't save the value of the select. The message was: The field "xxx" can not be empty. When I changed that field to type text, the result had been writed into the database table.

 

Finally,  I could resolve the problem. I could note the model had an attribute named: required=>true in the database creation field array. It was wrong. Here the correct code:

public static $definition = [
        'table'     => 'moduleTest',
        'primary'   => 'id_moduleTest',
        'multilang' => true,
//        'multilang_shop' => true,       
        'fields'    => ['id_moduleTest'    => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
                        'id_tienda'       => ['type' => self::TYPE_INT, 'db_type' => 'int'], 
                        'id_zona'         => ['type' => self::TYPE_INT, 'db_type' => 'int'], 
                        'asistente'       => ['type' => self::TYPE_STRING, 'db_type' => 'varchar(100)', 'validate' => 'isString', 'lang' => true],
                        'id_empleado'     => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'db_type' => 'int'], 
                        'comisionista'    => ['type' => self::TYPE_STRING, 'validate' => 'isString','db_type' => 'varchar(100)', 'lang' => true]
                        ],

Then, into the __construct() method I writed the instructions for the CRUD that I needed. Here my __construct() method into AdminModuleController class:

public function __construct()
    {
        $this->bootstrap  = true;
        $this->table      = 'moduleTest';
        $this->identifier = 'id_moduleTest';
        $this->className  = 'ModuleTestModel';
        
        parent::__construct();

        $id_lang = $this->context->language->id;
        
        
 AND b.id_lang = '. $id_lang. ')';
        $this->_join .= ' LEFT JOIN '._DB_PREFIX_.'moduleTest_lang b ON (b.id_contracts = a.id_moduleTest AND b.id_lang = '. $id_lang. ')';
        $this->_select .= ' b.asistente as asistente, b.comisionista as comisionista';

        $this->fields_list = [
            'id_moduleTest' => ['title' => $this->l('ID'), 'type'  => 'text', 'align' => 'center', 'class' => 'fixed-width-xs'],
            'id_tienda'    => ['title' => $this->l('Campaña'), 'type'  => 'int'],
            'id_zona'      => ['title' => $this->l('Zona'), 'type'  => 'text'],
            'asistente'    => ['title' => $this->l('Asistente'), 'type'  => 'text'],
            'id_empleado'  => ['title' => $this->l('Gerente'), 'type'  => 'text'],
            'comisionista' => ['title' => $this->l('Comisionista'), 'type'  => 'text'],
            ];
    
        $this->actions = ['edit', 'delete'];

        $this->bulk_actions = array(
            'delete' => array(
                'text'    => $this->l('Delete selected'),
                'icon'    => 'icon-trash',
                'confirm' => $this->l('Delete selected items?'),
            ),
        );
      
   
        $id_empleado = $this->context->employee->id;
       
        //fields to add/edit form
        
        $query = 'SELECT ID_ZONA as ID_ZONA, CODIGOZONA AS CODIGOZONA FROM `'._DB_PREFIX_.'zonas` where `id_employee`='.$id_empleado;
        $zonas = Db::getInstance()->query($query);
        $query_campana = 'SELECT id_shop AS id_campana, name AS campana FROM `'._DB_PREFIX_.'shop` WHERE `id_shop`>=2';
        $campana  = Db::getInstance()->query($query_campana);
 
   // The form field are: 

            $this->fields_form = [

            'legend' => ['title' => $this->l('Por favor ingresar los datos solicitados. Bienvenid@')],
            'input'  => ['comisionista'   => ['type'=>'text','label'=> $this->l('Comisionista'),'name' => 'comisionista', 'required' => true, 'lang' => true],
                         'asistente'   => ['type' => 'text', 'label'  => $this->l('Asistente'),'name' => 'asistente', 'required' => true, 'lang' => true], 
                         'id_zona'   => ['type' => 'select', 'label'  => $this->l('Zona'),'name' => 'id_zona', 'required' => true, 'options'=>array('query'=>$zonas, 'id' =>'ID_ZONA', 'name' => 'CODIGOZONA')],
                         'id_tienda'   => ['type' => 'select', 'label'  => $this->l('Campaña'),'name' => 'id_tienda', 'required' => true, 'options'=>array('query'=>$campana, 'id' => 'id_campana', 'name' => 'campana')],
                         'id_empleado'   => ['type' => 'text', 'label'  => $this->l('Gerente de Zona'),'name' => 'id_empleado',  'value'=>$id_empleado, 'placeholder'=>$id_empleado, 'disabled'=> true],
                        ],
            'submit' => ['title' => $this->l('Save'), 'name'=>'save'],
    
            ];
                      
    }       

 I didn't need another method for form processing.

Thanks for All. I hope this explanation will be useful for other developers.

 

Good Luck!!!

  • Thanks 1
Link to comment
Share on other sites

  • 6 months later...
On 25.01.2018 at 4:54 AM, Monika said:

 

Hi Ambassadors,

Thanks for your response. I did my module using the prestashop core, I maked this form to be use in the bakcoffice of Prestashop 1.7.  I didn't using a TPL, I maked the form writing an array in the moduleAdminController class, into de __construct() method. Well, when I was using a type select field, the form didn't save the value of the select. The message was: The field "xxx" can not be empty. When I changed that field to type text, the result had been writed into the database table.

 

Finally,  I could resolve the problem. I could note the model had an attribute named: required=>true in the database creation field array. It was wrong. Here the correct code:


public static $definition = [
        'table'     => 'moduleTest',
        'primary'   => 'id_moduleTest',
        'multilang' => true,
//        'multilang_shop' => true,       
        'fields'    => ['id_moduleTest'    => ['type' => self::TYPE_INT, 'validate' => 'isInt'],
                        'id_tienda'       => ['type' => self::TYPE_INT, 'db_type' => 'int'], 
                        'id_zona'         => ['type' => self::TYPE_INT, 'db_type' => 'int'], 
                        'asistente'       => ['type' => self::TYPE_STRING, 'db_type' => 'varchar(100)', 'validate' => 'isString', 'lang' => true],
                        'id_empleado'     => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'db_type' => 'int'], 
                        'comisionista'    => ['type' => self::TYPE_STRING, 'validate' => 'isString','db_type' => 'varchar(100)', 'lang' => true]
                        ],

Then, into the __construct() method I writed the instructions for the CRUD that I needed. Here my __construct() method into AdminModuleController class:


public function __construct()
    {
        $this->bootstrap  = true;
        $this->table      = 'moduleTest';
        $this->identifier = 'id_moduleTest';
        $this->className  = 'ModuleTestModel';
        
        parent::__construct();

        $id_lang = $this->context->language->id;
        
        
 AND b.id_lang = '. $id_lang. ')';
        $this->_join .= ' LEFT JOIN '._DB_PREFIX_.'moduleTest_lang b ON (b.id_contracts = a.id_moduleTest AND b.id_lang = '. $id_lang. ')';
        $this->_select .= ' b.asistente as asistente, b.comisionista as comisionista';

        $this->fields_list = [
            'id_moduleTest' => ['title' => $this->l('ID'), 'type'  => 'text', 'align' => 'center', 'class' => 'fixed-width-xs'],
            'id_tienda'    => ['title' => $this->l('Campaña'), 'type'  => 'int'],
            'id_zona'      => ['title' => $this->l('Zona'), 'type'  => 'text'],
            'asistente'    => ['title' => $this->l('Asistente'), 'type'  => 'text'],
            'id_empleado'  => ['title' => $this->l('Gerente'), 'type'  => 'text'],
            'comisionista' => ['title' => $this->l('Comisionista'), 'type'  => 'text'],
            ];
    
        $this->actions = ['edit', 'delete'];

        $this->bulk_actions = array(
            'delete' => array(
                'text'    => $this->l('Delete selected'),
                'icon'    => 'icon-trash',
                'confirm' => $this->l('Delete selected items?'),
            ),
        );
      
   
        $id_empleado = $this->context->employee->id;
       
        //fields to add/edit form
        
        $query = 'SELECT ID_ZONA as ID_ZONA, CODIGOZONA AS CODIGOZONA FROM `'._DB_PREFIX_.'zonas` where `id_employee`='.$id_empleado;
        $zonas = Db::getInstance()->query($query);
        $query_campana = 'SELECT id_shop AS id_campana, name AS campana FROM `'._DB_PREFIX_.'shop` WHERE `id_shop`>=2';
        $campana  = Db::getInstance()->query($query_campana);
 
   // The form field are: 

            $this->fields_form = [

            'legend' => ['title' => $this->l('Por favor ingresar los datos solicitados. Bienvenid@')],
            'input'  => ['comisionista'   => ['type'=>'text','label'=> $this->l('Comisionista'),'name' => 'comisionista', 'required' => true, 'lang' => true],
                         'asistente'   => ['type' => 'text', 'label'  => $this->l('Asistente'),'name' => 'asistente', 'required' => true, 'lang' => true], 
                         'id_zona'   => ['type' => 'select', 'label'  => $this->l('Zona'),'name' => 'id_zona', 'required' => true, 'options'=>array('query'=>$zonas, 'id' =>'ID_ZONA', 'name' => 'CODIGOZONA')],
                         'id_tienda'   => ['type' => 'select', 'label'  => $this->l('Campaña'),'name' => 'id_tienda', 'required' => true, 'options'=>array('query'=>$campana, 'id' => 'id_campana', 'name' => 'campana')],
                         'id_empleado'   => ['type' => 'text', 'label'  => $this->l('Gerente de Zona'),'name' => 'id_empleado',  'value'=>$id_empleado, 'placeholder'=>$id_empleado, 'disabled'=> true],
                        ],
            'submit' => ['title' => $this->l('Save'), 'name'=>'save'],
    
            ];
                      
    }       

 I didn't need another method for form processing.

Thanks for All. I hope this explanation will be useful for other developers.

 

Good Luck!!!

 

Monika, you saved my day! Thanks a lot!!!

Cheers ;)

Link to comment
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
×
×
  • Create New...