It looks like you're new here. If you want to get involved, click one of these buttons!
I am trying to create a form that site non-priviledged users would fill (not in admin area). I am using form_builder library and I cannot see how to repopulate the form if validation returns errors. I get an empty form instead of repopulated... I have done forms in codeigniter, but I would like to use the advanced features of fuelcms.
Here is my controller code, the important function is "create":
<?php
//require_once(FUEL_PATH.'/libraries/Fuel_pages.php');
require_once(FUEL_PATH.'/libraries/Fuel_base_controller.php');
class Lovedones extends CI_controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
public function index()
{
//$this->fuel->pages->render('pages', $vars);
echo "Čia turi būti mirusių artimųjų sąrašas.";
}
// šitam sukurtas specialus maršrutas
public function create()
{
/**** ĮKELIAM BIBLIOTEKAS, NUSTATOM PARINKTIS ****/
$this->load->library('form_builder');
$this->form_builder->show_required = FALSE; //nerodyti žvaigždutės, nes prie jos neprilimpa joks tekstas
$this->form_builder->label_layout = 'top';
//$this->form_builder->required_indicator = '+';
//$this->form_builder->show_required = '<span class="required">{required_indicator}</span> šie laukai yra privalomi'; // šito neklauso!
//$this->form_builder->load_custom_fields(APPPATH.'config/custom_fields.php');
$this->load->library('form_validation');
$this->load->model('Dearones_model');
/**** LOADING LIBRARIES AND SETTING PREFERENCES ****/
$fields['email'] = array('type' => 'email', 'placeholder' => 'vardenis@pavardenis.lt', 'label'=>'El. pašto adresas', 'required' => TRUE);
$fields['name'] = array('type' => '','label'=>'Jūsų vardas ar pseudonimas');
$fields['name_of_dearone'] = array('type' => '','label'=>'Artimojo vardas', 'required' => TRUE);
$fields['year_birth'] = array('type' => 'date','date_format' => 'Y', 'label'=>'Gimimo metai', 'size'=>'10', 'min_date' => '1900', 'max_date' => date('Y'));
$fields['year_death'] = array('type' => 'date','date_format' => 'Y', 'label'=>'Netekties metai', 'size'=>'10', 'min_date' => '1950', 'max_date' => '2018', 'required' => TRUE);
// laikinai, kol visa kita veiks, nes nežinau, kaip daryti su paveikslėliais - išjungiu image
//$fields['image'] = array('type' => 'file', 'folder' => 'assets/images/dearones/','label'=>'Artimojo nuotrauka', 'upload' => TRUE,'file_name' => '{id}', /*'encrypt_name'=>'TRUE',*/ 'display_preview'=>TRUE, 'height'=>'600','maintain_ratio'=>TRUE);
$image_default_options = array(
//'0' => 'Nepasirinkta',
'1' => '<img src="' . img_path("default/1.jpg") . '" alt="Foto1" height="52">',
'2' => '<img src="' . img_path("default/2.jpg") . '" alt="Foto2" height="52">',
'3' => '<img src="' . img_path("default/3.jpg") . '" alt="Foto3" height="52">',
'4' => '<img src="' . img_path("default/4.jpg") . '" alt="Foto4" height="52">',
'5' => '<img src="' . img_path("default/5.jpg") . '" alt="Foto5" height="52">',
'6' => '<img src="' . img_path("default/6.jpg") . '" alt="Foto6" height="52">',
'7' => '<img src="' . img_path("default/7.jpg") . '" alt="Foto7" height="52">',
'8' => '<img src="' . img_path("default/8.jpg") . '" alt="Foto8" height="52">',
'9' => '<img src="' . img_path("default/9.jpg") . '" alt="Foto9" height="52">',
'10' => '<img src="' . img_path("default/10.jpg") . '" alt="Foto10" height="52">',
'11' => '<img src="' . img_path("default/11.jpg") . '" alt="Foto11" height="52">',
'12' => '<img src="' . img_path("default/12.jpg") . '" alt="Foto12" height="52">',
'13' => '<img src="' . img_path("default/13.jpg") . '" alt="Foto13" height="52">',
);
$fields['image_default'] = array('type' => 'enum', 'mode' => 'radios', 'options' => $image_default_options, 'label'=>'Paveikslėlis vietoje artimojo nuotraukos', 'null' => TRUE); //TODO: panaikinti tą default, prikabinti paveikslėlių miniatiūras
$fields['content'] = array('type' => 'textarea','label'=>'Čia galite parašyti daugiau apie tą kurio netekote ');
$this->form_builder->set_fields($fields);
/**** FORMOS VALIDAVIMO TAISYKLĖS ****/
$this->form_validation->set_rules('email','El. pašto adresas','trim|valid_email|max_length[256]|required|xss_clean');
$this->form_validation->set_rules('name','Jūsų vardas ar pseudonimas','trim|max_length[256]|required|xss_clean');
$this->form_validation->set_rules('name_of_dearone','Artimojo vardas','trim|max_length[256]|required|xss_clean');
$this->form_validation->set_rules('year_birth','Gimimo metai','trim|integer|max_length[4]|min_length[4]'); //papildyti pagal fields parametrus
$this->form_validation->set_rules('year_death','Netekties metai','trim|integer|max_length[4]|min_length[4]|less_than_equal_to['.date('Y').']');
// kaip dėl upload lauko? ką tikrinti?
$this->form_validation->set_rules('image_default','Paveikslėlis vietoje artimojo nuotraukos','trim|integer|less_than[14]|greater_than[0]');
$this->form_validation->set_rules('content','Čia galite parašyti daugiau apie tą kurio netekote','trim|xss_clean|max_length[3000]');
if($this->form_validation->run()) //jei validacija pavyko
{
$params = array(
'email' => $this->input->post('email'),
'name' => $this->input->post('name'),
'name_of_dearone' => $this->input->post('name_of_dearone'),
'year_birth' => $this->input->post('year_birth'),
'year_death' => $this->input->post('year_death'),
//'image' => $this->input->post('image'), // laikinai, kol visa kita veiks, nes nežinau, kaip daryti su paveikslėliais
'image_default' => $this->input->post('image_default'),
'content' => $this->input->post('content'),
'id_random' => url_title(substr(sha1(rand()), 0, 30), 'dash', TRUE),
'type' => 'humans', // alternatyva: animals
'published' => 'no'
);
$result = $this->Dearones_model->insert($params);
$record_id = $this->db->insert_id(); var_dump($record_id); exit;
if($result == '1' && $record_id != '0')
{
$msg = array(
'text' => 'Duomenys sėkmingai įrašyti.',
'class' => 'success' // success info warning danger
);
$this->session->set_flashdata('message',$msg);
redirect('lovedones/create'); // TODO: PAKEISTI Į INDEX HUMANS VĖLIAU
}
else
{
//jei duomenų nepavyko įrašyti dėl nežinomų priežasčių (greičiausiai db problema):
$msg = array(
'text' => 'Duomenų nepavyko įrašyti, matyt duomenų bazės klaida.',
'class' => 'danger' // success info warning danger
);
$this->session->set_flashdata('message',$msg);
$vars['form'] = $this->form_builder->render();
$this->fuel->pages->render('lovedones/create', $vars);
}
}
else
{
//var_dump($this->form_validation->error_array()); exit;
if(!is_null($this->input->post('Submit')))
{
$msg = array(
'text' => 'Duomenyse yra klaidų, ištaisykite jas.',
'class' => 'warning' // success info warning danger
);
$this->session->set_flashdata('message',$msg);
}
echo 'trečias';
$vars['form'] = $this->form_builder->render();
$this->fuel->pages->render('lovedones/create', $vars);
}
}
}
Comments
This is a comment I could not delete, so it remains
There is a method called "set_field_values" that you can pass an array of the previously posted values.
Yes, thank you, how could I have overlooked that... Could you also explain, how to get the form to display errors? I see there is $this->form_builder->validate(['$validator'=NULL]) and related methods, and I should perhaps use them instead of the codeigniter validation class, but it is not clear, how to do that from the documentation