Extension, Plugin

Archived
Forum
(read-only)


For official support, visit the official support site »

Custom System Messages

ExpressionEngine 2

Back to this add-on's main page
View Other Add-ons From Brian Litzinger

     

CSM with custom module

General

Joe Dixon
Joe Dixon

Hi Brian,

I have built a series of modules that I’d really like to use with CSM. At the moment, I am simply throwing an error in the usual way:

return $this->EE->output->show_user_error('general'$form_errors); 

As I understand it, in order for this to work with Ajax, I’d need to send a JSON response? Do you have any pointers on how to set this up?

Thanks in advance,
Joe

litzinger
# 1
Developer
litzinger

Hi there Joe. You would basically need to do something like this, taken from the User module:

if ($this->is_ajax_request())
   
{
    $this
->send_ajax_response(array(
     
'success' => TRUE
     
'heading' => lang('user_successful_submission'),
     
'message' => lang('password_changed'),
     
'content' => lang('password_changed')
    ));
    exit();
   
}
  
   
return ee()->output->show_message(array(
    
'title'  => ee()->lang->line('success'), 
    
'heading' => ee()->lang->line('success'), 
    
'link'  => array( $returnee()->lang->line('return') ), 
    
'content' => ee()->lang->line('password_changed')
   )); 

Ideally the show_user_error method would handle that in the EE core, but it isn’t there…. yet.

CSM handles ajax responses in this way:

if(
            
$this->EE->input->is_ajax_request() AND
            ( !isset(
$settings['enable_ajax']) OR ( isset($settings['enable_ajax']) AND $settings['enable_ajax'== 'y') )
        )
        
{       
            
if(isset($data['type']) AND $data['type'=== false)
            
{
                $data[
'success'true;
            
}
            
else
            
{
                $data[
'success'false;
            
}

            $this
->EE->output->send_ajax_response($data);
            exit;
        

Basically don’t just resort to the show_user_error method, test for an ajax request first, then use send_ajax_response or show_user_error depending on the case.

Joe Dixon
# 2
Joe Dixon

Hi,

Thanks for your reply, unfortunately, it’s not showing up as an ajax request and defaulting to showing the user error. Does that mean there is something wrong in the setup of my form?

Thanks,
Joe

litzinger
# 3
Developer
litzinger

Its hard to say without seeing the forms. Are you using jQuery or something similar to submit the form via ajax?

Joe Dixon
# 4
Joe Dixon

Hi Brian,

It’s just a bog standard form. I would like the user to submit with the submit button as normal, but return the error to the same page. I may be missing something, but I don’t see the User module submitting with ajax? Am I missing something simple? It’s been a long week ;)

Thanks,
Joe

litzinger
# 5
Developer
litzinger

I see what you are saying. This can be done by using the ACT drop down options in CSM to set a template for a specific action. So setup your form template similar to these settings (in this example I’m using a registration form): https://skitch.com/litzinger/gpq8j/extension-settings-expressionengine

Then in that template you’ll use the conditionals to either display the error or the form, or both.

{if csm:error}
    {heading}
    {content}
{
/if}

<form>
 ... 
fields ...
</
form

This will basically be setting your registration template up as the “error” template. So under normal conditions when a user first goes to the form the conditional will be false, and it will show the form as intended. If there is an error, CSM will load the same registration template, but this time show the error message above it. Make sense?

Joe Dixon
# 6
Joe Dixon

Thanks Brian,

That seems to have done it for most of my forms - I just have a couple to play with as they are currently returning the default EE error page rather than sending back to the page with the form or even my own custom error page.

Thanks for your help,
Joe