79 lines
2.0 KiB
PHP
79 lines
2.0 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class DynamicForm extends CI_Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
// 👉 ICI : chargement de la base de données
|
|
$this->load->database();
|
|
|
|
// chargements nécessaires
|
|
$this->load->model('Form_fields_model');
|
|
$this->load->library('form_validation');
|
|
$this->load->helper(['form', 'url']);
|
|
}
|
|
|
|
public function show($form_key)
|
|
{
|
|
$fields = $this->Form_fields_model->get_active_fields($form_key);
|
|
|
|
if (empty($fields)) {
|
|
show_error('Formulaire introuvable', 404);
|
|
}
|
|
|
|
foreach ($fields as $f) {
|
|
if (!empty($f['rules'])) {
|
|
$this->form_validation->set_rules(
|
|
$f['name'],
|
|
$f['label'],
|
|
$f['rules']
|
|
);
|
|
}
|
|
}
|
|
|
|
$this->load->view('dynamic_form', [
|
|
'form_key' => $form_key,
|
|
'fields' => $fields
|
|
]);
|
|
}
|
|
|
|
public function submit($form_key)
|
|
{
|
|
$fields = $this->Form_fields_model->get_active_fields($form_key);
|
|
|
|
foreach ($fields as $f) {
|
|
if (!empty($f['rules'])) {
|
|
$this->form_validation->set_rules(
|
|
$f['name'],
|
|
$f['label'],
|
|
$f['rules']
|
|
);
|
|
}
|
|
}
|
|
|
|
if ($this->form_validation->run() === FALSE) {
|
|
$this->show($form_key);
|
|
return;
|
|
}
|
|
|
|
// 👇 ICI on sauvegarde
|
|
$data = [];
|
|
foreach ($fields as $f) {
|
|
$name = $f['name'];
|
|
$data[$name] = $this->input->post($name);
|
|
}
|
|
|
|
$this->db->insert('form_data_test', [
|
|
'form_key' => $form_key,
|
|
'data' => json_encode($data, JSON_UNESCAPED_UNICODE)
|
|
]);
|
|
|
|
echo 'OK : données sauvegardées';
|
|
exit;
|
|
}
|
|
|
|
}
|