I have a couple of controllers on my site that process form data. Forms use AJAX, and I have several methods for different controllers that have to do some processing to return errors in JSON encoded format - see the code below. Obviously, this is NOT DRY, and I need to move this code into one helper function that I can use globally, but I wonder where it should go! Should I create a static helper class that contains this function (e.g. Validation :: build_ajax_errors ()), or since this code creates a format that is application specific and related to the jQuery validation plugin that I use, should it be static method, stored, for example, in my main site controller,from which form controllers are distributed?
if (request::is_ajax())
{
$errors = $post->errors('form_data/form_error_messages');
$i = 0;
$new_errors = array();
foreach ($errors as $key => $value)
{
$new_errors[$i][0] = '#' . $key;
$new_errors[$i][1] = $value;
$new_errors[$i][2] = "error";
$i++;
}
echo '{"jsonValidateReturn":' . json_encode($new_errors) . '}';
return;
}