Hi, I have a couple of controllers on my site which are handling form data. The forms use AJAX and I have quite a few methods across different controllers which are having to do some specific processing to return errors in a JSON encoded format - see code below. Obviously this isn't DRY and I need to move this code into a single helper function which I can use globally, but I'm wondering where this should actually go! Should I create a static helper class which contains this function (e.g Validation::build_ajax_errors()), or as this code is producing a format which is application specific and tied into the jQuery validation plugin I'm using, should it be a static method stored in, for example, my main Website controller which the form handling controllers extend from?
//if ajax request, output errors
if (request::is_ajax())
{
//need to build errors into array form for javascript validation - move this into a helper method accessible globally
$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;
}