[eluser]froddd[/eluser]
Hey all,
I'm having an issue updating some code to use the 'form_validation' library instead of the deprecated 'validation' one...
The main problem here is that I'm loading the form itself as a separate view, as I'm keen on using the same HTML for both adding new records and editing existing ones.
With the old 'validation' library, my code would look similar to this in the controller:
Code:
function add()
{
$client = new Client();
$this->load->library('validation');
// general validation stuff
$this->validation->set_error_delimiters('<label class="errorbg">', '</label>');
// set rules
$rules['name'] = "required";
$rules['email'] = "valid_email";
$this->validation->set_rules($rules);
// set messages
$fields['name'] = 'Name';
$fields['email'] = 'Email';
$this->validation->set_fields($fields);
if( $this->validation->run() ){
// process the data
$client->name = $this->input->post('name');
$client->email = $this->input->post('email');
if( $client->save() ){
$this->session->set_flashdata('action', $client->name . ' has been added to your clients');
redirect('/clients');
}
} else {
$formdata['error'] = '';
}
$formdata['form_action'] = 'add';
$formdata['validation'] = $this->validation;
// load the form into the view
$data['form'] = $this->load->view('clients/form', $formdata, true);
// general page variables
$data['page_title'] = 'Clients';
$this->load->view('inc/header', $data);
$this->load->view('clients/add', $data);
$this->load->view('inc/footer');
}
And the view for the form:
Code:
<?=$error?>
<form action="/clients/<?=$form_action?>" method="post">
<p><label for="name">Name</label> <input id="name" name="name" type="text" value="<?=$validation->name?>" /><?=$validation->name_error?></p>
<p><label for="email">Email</label> <input id="email" name="email" type="text" value="<?=$validation->email?>" /><?=$validation->email_error?></p>
<p><button type="submit">Save client</button> - <a href="/clients">Cancel</a></p>
</form>
As I was then able to pass $this->validation as a view variable I could then show the values and errors as validation properties. However, with the 'form_validation' library, I cannot do so as to show values and errors I need to use set_value() and form_error()... Which I can't seem to be able to pass to the 'second-level' form view.
Any idea? I've quickly copy-pasted some code while trying to remove unneeded fields, so if there's any typo in this I'm sorry, ignore them!
Thanks for any help :-P