OOP/MVC advice on where to place a global helper function

Posted by franko75 on Stack Overflow See other posts from Stack Overflow or by franko75
Published on 2010-05-11T09:58:15Z Indexed on 2010/05/11 10:04 UTC
Read the original article Hit count: 217

Filed under:
|
|

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;
                }

© Stack Overflow or respective owner

Related posts about php

Related posts about mvc