[eluser]Maglok[/eluser]
I have to make forms a lot in webapplications. I usually just make a form in a function and then create the code again if I need an edit form.
Example: I have create user and edit user. Both are basically the same form, but the edit form has preset values from the database.
I have a private function that makes the form for me at the moment. I just can't seem to get it to handle both default values and database values simply.
Code:
private function form_account() {
$this->config->load('pona');
$permissions = $this->config->item('pona_permissions');
$faculties = $this->config->item('pona_faculties');
$this->form
->text('first_name', 'First name', 'required|max_length[40]')
->text('last_name', 'Last name', 'required|max_length[40]')
->text('email', 'Email address', 'required|max_length[40]|valid_email')
->select('permission', $permissions, 'Access level', '1', 'required')
->select('faculty', $faculties, 'Faculty', '1', 'required')
->password('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') .']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]')
->password('password_confirm', 'Password confirmation', 'required')
->submit('Create');
}
This is using the Form Generation Library library, but it hooks to the CI one so that shouldn't matter.
I can add something crude like parameter with database info, if it is set then display the form with database values, otherwise show default values, but that would required a big if statement that would still require me to write out the form twice and it kinda defeats the purpose.
It's probably a thing people encountered before, I just can't seem to think of a quick way to be able to say:
Code:
$this->form
->text('first_name', 'First name', 'required|max_length[40]', $accountinfo->first_name)
And it would automatically NOT fill that out if it wasn't filled in. Instead it throws me errors, because $accountinfo->first_name is not set.