[eluser]Colin Williams[/eluser]
Randy's right that it's a simple fix, although there really should be a set_values() method in the Validation library. (I think it's been done in the wiki. I've done it but I don't know if it's worthy of a release because it catered rather particularly to the given app.)
I have a config/forms.php settings file that looks something like this:
Code:
$config['forms']['register']['rules'] = array(
'name' => 'required',
'password' => 'required',
);
$config['forms']['register']['fields'] = array(
'name' => 'Your Username',
'password' => 'Set a Password',
);
$config['forms']['register']['values'] = array(
'name' => 'Bob',
'password' => '',
);
Later on, at the Controller...
Code:
$this->load->library('validation');
$form = $this->config->item('forms', 'register');
$this->validation->set_fields($form['fields']);
$this->validation->set_rules($form['rules']);
// yadda yadda yadda
$this->load->view('user/register', $form);
So, now in my view, I have $rules, $fields (good for labels) and, ahah!, $values. And this way, I'm never accessing $this->validation in my View (I avoid $this->anything in my views).
Ah, but how do the values get set if the user has input some? Like this:
Code:
foreach ($form['fields'] as $name => $value)
{
if (is_array($form['values'][$name]) and !is_array($this->validation->$name))
{
// For empty multiple valued fields
$form['values'][$name] = array();
}
else
{
$form['values'][$name] = $this->validation->$name;
}
}
And this little loop is what should be done in $this->validation->set_values(), well, the other way around... maybe there should be get_values() too, since that is really what my loop is doing. set_values() would store default values in $this->validation->name