Get
The following method will return the html output of the generated form as a string.
Note: The get() method will also validate the form data.
get ()
You can either handle all validation and database queries as well as a redirect upon successfull validation with the model() and onsuccess() functions...
$this->load->library('form');
$this->form
->text('username', 'Username')
->text('password', 'Password')
->submit()
->model('user', 'login_user')
->onsuccess('redirect', 'logged_in');
$data['form'] = $this->form->get();
$data['errors'] = $this->form->errors;
$this->load->view('login', $data);
The model will only be called if the form was submit and valid. The onsuccess method will only be executed if the form was submit and valid and the model didn't add any errors.
...or you can process some more code subject to validation after the get() method was called:
$this->load->library('form');
$this->form
->text('username', 'Username')
->text('password', 'Password')
->submit()
->model('user', 'login_user');
$data['form'] = $this->form->get();
if ($this->form->valid)
{
// do stuff
redirect('logged_in');
}
else
{
$data['errors'] = $this->form->errors;
}
$this->load->view('login', $data);
Alternatively you can call get() after the if clause. You will then need to validate the form manually instead. This can be useful if you want to run some code in between which might flag another error or add some more elements to the form.