Php – Is it Possible to Apply Multiple String Interpolation in Labels for Form Validation Custom Callback Methods in CodeIgniter

codeigniterphpvalidation

Here's an approximate form of my current code; trimmed down to the essentials of the problem using CodeIgniter 1.7:

In system/application/config/form_validation.php, I have a rule that looks like the following:

'some_controller/save' => array(
    array(
        'field' => 'some_code',
        'label' => 'Some Code Name',
        'rules' => 'trim|required|min_length[1]|max_length[6]|callback__unique_codename'
    ),
),

In system/application/controllers/some_controller.php, I have the custom callback function needed by the above validation rule:

function _unique_codename($codename)
{
    $result = $this->some_code_model->find_by_codename($codename); // this returns NULL if the codename is not found
    if ($result)
    {
        $this->form_validation->set_message('_unique_codename', '%s already exists. Please enter another %s.');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

After form submission an error occurs. The error displayed is the following: "Some Code Name already exists. Please enter another."

Is there any way to evaluate multiple %s instances in the error message?

Best Solution

you're running _unique_codename only on the some_code field right? if thats the case, what do you mean by multiple instances of %s?

do you want to run this in a loop and collect all the errors to be displayed at the same time?

Related Question