[eluser]Colin Williams[/eluser]
If you haven't done it yet, I got this working.
Code:
<?php
class MY_Validation extends CI_Validation {
function MY_Validation()
{
parent::CI_Validation();
}
function set_errors($fields)
{
if (is_array($fields) and count($fields))
{
foreach($fields as $key => $val)
{
$error = $key.'_error';
if (isset($this->$error) and isset($this->$key) and $this->$error != '')
{
$old_error = $this->$error;
$new_error = $this->_error_prefix.sprintf($val, $this->$key).$this->_error_suffix;
$this->error_string = str_replace($old_error, $new_error, $this->error_string);
$this->$error = $new_error;
}
}
}
}
}
This creates
$this->validation->set_errors(). Because the run() method generates the error string based on your rules, this method MUST be called AFTER $this->validation->run() and BEFORE your store or print $this->validation->error_string:
Code:
[...]
// Run Validation
if (!$this->validation->run())
{
// Set custom errors
$this->validation->set_errors(array('name' => 'Common now!'));
if (!empty($this->validation->error_string))
// Validation ran and there was an error
{
$form['error'] = $this->validation->error_string;
[...]
I admit, it's a bit awkward using str_replace(), but I'd never want to override the run() method, which generates errors based on rules and not fields.
Also note that your supplied messages get sprintf'd, so you can include the submitted value in your error!
Code:
$this->validation->set_errors(array('email' => "'%s' is not a valid email address."));
// "'colin@website' is not a valid email address."
Remember to use %1$s if the field value appears more than once in your message.