I'm creating a form with validation in CodeIgniter, and I have problem with mixing proccessing form with validation. In my form I have checkboxes that are generated from database. All works, when someone submit form without mistake (no validation failure), but after failed validation my checkboxes are not generated correctly (I have errors).
My Controller:
public function form()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
/////////////////////////////////////////
////// MY PROCESSING CODE //////////////
////////////////////////////////////////
//Generating checkbocks from database
$data['szkolenia'] = $this->Szkolenie_m->pobierz();
//Validation
$this->form_validation->set_rules('imie', 'Imię', 'required');
$this->form_validation->set_message('required', 'Błąd: wypełnij powyższe pole');
//After form submit
if (!empty($_POST))
{
$konsultant = $this->uri->segment(3);
$dane = array(
'email1' => $this->input->post('email'),
'imie' => $this->input->post('imie'),
'nazwisko' => $this->input->post('nazwisko'),
'nazwa_firmy' => $this->input->post('firma'),
'konsultant_id' => $konsultant,
);
//Saving selected checkboxes into another db
$boxes = $_POST['formChecks'];
$N = count($boxes);
$ostatni_id = $this->Osoby_m->ostatni_id();
for($i=0; $i < $N; $i++)
{
$this->Osoby_m->nowy_wpis_formularz($boxes[$i]);
//echo $boxes[$i];
}
//Adding data to DB
$this->Osoby_m->nowa_osoba($dane);
}
/////////////////////////////////////////
////// PROCESSING CODE END //////////////
////////////////////////////////////////
if ($this->form_validation->run() == FALSE)
{
$this->load->view('formularz', $data);
}
else
{
$this->load->view('wyslano_formularz');
}
}
My checkboxes in View:
<?php foreach ($szkolenia as $szkolenie): ?>
<div class="checkbox">
<label><input id="szkolenie<?php echo $szkolenie->id; ?>" type="checkbox" name="formChecks[]" value="<?php echo set_value('formChecks[]'); ?>"> <?php echo $szkolenie->nazwa_szkolenia; ?></label>
<br>
</div>
<?php endforeach; ?>
After submiting this form with blank required input script doesnt reload checkboxes from database and display an error. This is happening event, when I put $data['szkolenia'] = $this->Szkolenie_m->pobierz();
into
if ($this->form_validation->run() == FALSE) { $this->load->view('formularz', $data); }
This is error I get:
If you want to load checkboxes on validation error,
use set_checkbox('name', 'value');
Like this,
set_checkbox('formChecks', 'value');
Getting error because, you are trying to echo an array.