ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Custom error handling

December 01, 2009 1:59pm

Subscribe [3]
  • #1 / Dec 01, 2009 1:59pm

    Mark Croxton's avatar

    Mark Croxton

    319 posts

    I would like to intercept the expression engine error messages before they are output to the browser. My idea is to store the error message in the session, redirect to the original page (the return to link), grab the message from the session and display it on the original page. This would allow inline error messages.

    I’ve got this working easily for freeform using the extension hooks freeform_module_validate_end and freeform_module_form_end (see attached). I would now like to get this to work for member registration, login and profile forms. Unfortunately there are no hooks that I can find that intercept the form processing at the validation and form rendering stages. Also, frustratingly there are no hooks for show_message and show_user_error in the core output class.

    Are there any hooks that I’ve missed that might be used for this purpose? If not, is it possible to overload the core classes like you can in CI? Any other ideas?

  • #2 / Dec 01, 2009 2:16pm

    ender's avatar

    ender

    1644 posts

    I’m not aware of any hooks or other server-side techniques to easily do that… I would suggest going with javascript enhancement in the form of an ajax form post to do this.

  • #3 / Dec 01, 2009 2:30pm

    Mark Croxton's avatar

    Mark Croxton

    319 posts

    EE’s form processing outputs errors as html (hardcoded unordered lists - bad style) - sadly there doesn’t appear to be a json mode; I guess I could submit the form via ajax and return the output to a div, but really I would like to highlight the individual error fields.

  • #4 / Dec 01, 2009 2:56pm

    ender's avatar

    ender

    1644 posts

    just create a jquery object with the returned html and use the traversal/manipulation tools to get what you need out of it.  XPATH is also probably an option if you prefer it.

  • #5 / Dec 01, 2009 4:29pm

    Focus Lab Dev Team's avatar

    Focus Lab Dev Team

    1129 posts

    I’ve got this working easily for freeform using the extension hooks freeform_module_validate_end and freeform_module_form_end

    Sounds handy. Are you intending to share said extension? 😊

  • #6 / Dec 02, 2009 7:57am

    Mark Croxton's avatar

    Mark Croxton

    319 posts

    Cracked it, and I didn’t need to resort to ajax or hack the core 😊

    I’m using the User module from Solspace and I rewrote _output_error function in mod.user.php to work with my form validation/error handling extension. The new function looks like this:

    function _output_error($type, $errors)
        {
            global $LANG, $IN;
            
            switch($type)
            {
                case 'submission' : $heading = $LANG->line('submission_error');
                    break;
                case 'general'    : $heading = $LANG->line('general_error');
                    break;
                default           : $heading = $LANG->line('submission_error');
                    break;
            }
            
            // if no active session we start a new one
            if (session_id() == "") 
            {
                session_start(); 
            }
            
            // save errors and form data to the session
            $_SESSION['form_error_type'] = $heading;
            $_SESSION['form_validation_errors'] = $errors;
            $_SESSION['form_data'] = $_POST;
            
            // redraw form and exit
            $return_uri = $IN->GBL('caller', 'POST');
            header( "Location:".$return_uri);
            
            exit;        
        }

    I’ve got a bit more to do on the extension and will release it when I’ve tested it properly.

  • #7 / Dec 21, 2009 6:18pm

    Rob Sanchez's avatar

    Rob Sanchez

    335 posts

    I’ve used jQuery and ajax to get around this in the past:

    $.post(
        '{path=member/update_profile}',
        {
            m_field_id_1: $('#profile_first_name').val(),
            m_field_id_2: $('#profile_last_name').val(),
            m_field_id_3: $('#profile_id').val()
        },
        function(data)
        {
            if (data.match(/<title>Error<\/title>/))
            {
                $.facebox(data.replace('id="content"',''));
            }
            else
            {
                // show a success message
                $.facebox('Profile Updated Successfully');
            }
        },
        'text'
    );

    You could also pretty easily modify the User Message specialty template to output some JSON.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases