📝 HelperForm — formularios legacy en el Admin
Actualizado: 2024-12-01
HelperForm es el sistema legacy de PrestaShop para crear formularios en el Back Office. Aunque PrestaShop 8+ promueve el uso de Symfony Forms, HelperForm sigue siendo valido y funcional, especialmente para modulos que necesitan compatibilidad con PS 1.7.
#Estructura basica de HelperForm
Estructura minima de un HelperForm
php
<?php
// En getContent() o en un AdminController
private function renderForm(): string
{
$helper = new HelperForm();
// Configuracion del helper
$helper->table = $this->name;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->submit_action = 'submit' . $this->name;
$helper->default_form_language = $this->context->language->id;
// Pre-rellenar campos con valores guardados
$helper->fields_value = [
'MY_MODULE_API_KEY' => Configuration::get('MY_MODULE_API_KEY', ''),
'MY_MODULE_ACTIVE' => Configuration::get('MY_MODULE_ACTIVE', false),
'MY_MODULE_MODE' => Configuration::get('MY_MODULE_MODE', 'sandbox'),
];
return $helper->generateForm([$this->getFormFields()]);
}
private function getFormFields(): array
{
return [
'form' => [
'legend' => [
'title' => $this->l('Configuracion del modulo'),
'icon' => 'icon-cogs',
],
'input' => [
[
'type' => 'text',
'label' => $this->l('API Key'),
'name' => 'MY_MODULE_API_KEY',
'required' => true,
'desc' => $this->l('Clave de API proporcionada por el proveedor'),
],
[
'type' => 'switch',
'label' => $this->l('Activar modulo'),
'name' => 'MY_MODULE_ACTIVE',
'values' => [
['id' => 'active_on', 'value' => 1, 'label' => $this->l('Si')],
['id' => 'active_off', 'value' => 0, 'label' => $this->l('No')],
],
],
[
'type' => 'select',
'label' => $this->l('Modo'),
'name' => 'MY_MODULE_MODE',
'options' => [
'query' => [
['id' => 'sandbox', 'name' => $this->l('Pruebas (Sandbox)')],
['id' => 'production', 'name' => $this->l('Produccion')],
],
'id' => 'id',
'name' => 'name',
],
],
],
'submit' => [
'title' => $this->l('Guardar'),
'class' => 'btn btn-default pull-right',
],
],
];
}
#Tipos de campo disponibles
| Tipo | Descripcion | Opciones especiales |
|---|---|---|
| text | Campo de texto simple | required, desc, suffix, prefix |
| textarea | Area de texto multilinea | rows, cols, autoload_rte (TinyMCE) |
| switch | Toggle on/off | values: array con id/value/label |
| select | Lista desplegable | options.query, options.id, options.name |
| checkbox | Casilla de verificacion | values: array de opciones |
| radio | Botones de radio | values: array de opciones |
| color | Selector de color (colorpicker) | - |
| file | Subida de archivo | display_image para imagenes |
| password | Campo password (oculto) | - |
| hidden | Campo oculto | - |
#Guardar con Configuration
Procesar y guardar el formulario en getContent()
php
<?php
public function getContent(): string
{
$output = '';
// Procesar submit
if (Tools::isSubmit('submit' . $this->name)) {
$apiKey = Tools::getValue('MY_MODULE_API_KEY');
$active = (int) Tools::getValue('MY_MODULE_ACTIVE');
$mode = Tools::getValue('MY_MODULE_MODE');
// Validacion
if (empty($apiKey)) {
$output .= $this->displayError($this->l('La API Key no puede estar vacia.'));
} else {
// Guardar en Configuration
Configuration::updateValue('MY_MODULE_API_KEY', $apiKey);
Configuration::updateValue('MY_MODULE_ACTIVE', $active);
Configuration::updateValue('MY_MODULE_MODE', $mode);
$output .= $this->displayConfirmation($this->l('Configuracion guardada correctamente.'));
}
}
return $output . $this->renderForm();
}
#Diferencias con Symfony Form
| Aspecto | HelperForm (legacy) | Symfony Form (moderno) |
|---|---|---|
| Compatibilidad | PS 1.5 - PS 9 | PS 1.7.6+ (PS 8 recomendado) |
| Complejidad | Simple, directo | Mas estructurado, mas codigo |
| Validacion | Manual con Tools::getValue | Automatica con Constraints |
| UI | Bootstrap PS legacy | Identica al core de PS 8 |
| CSRF | Token manual | Automatico |
| Recomendado para | Modulos PS 1.7 compatibles | Modulos PS 8+ exclusivos |
Descargar en Markdown
Pensado para pegar en ChatGPT, Claude u otra IA. Incluye solo el contenido de esta pagina.