'undefined' alert when saving a custom block

Permalink
Hi All,

I'm having a problem with a custom block type I created. Clicking the 'Update' or 'Add' button when editing/creating a block results in a javascript alert that simply says 'undefined'. This error occurs with Internet Explorer no matter what data is entered into the fields. With Firefox it shows the alert only if the first fields is left blank, otherwise it works fine.

I've tried delving into the javascript code that controls the ccm-form-submit-button, but it is quite difficult to see what's happening and where.

Does anyone know what could be causing this? Any tips where and what to look for?

Thanks for your help.

Steve

 
jheanly replied on at Permalink Reply
jheanly
Hi Steve,

I know this is an old post but I came across the same problem recently and managed to work out what I was doing wrong. So, I thought I would share my solution in the event that someone else finds your post and is looking for a solution.

As part of a block add/edit, you can set up some basic field validation. The validation is triggered by two components.

In your controller.php file, there is a function called getJavaScriptStrings() that defines the error messages to be displayed when mandatory fields are not entered. Below is an example of the getJavaScriptStrings() function in my block...

public function getJavaScriptStrings() 
{
    return array('slideDuration-required' => t('Please enter a numeric Slide Duration. This controls for how long each image is displayed before transitioning to the next image.'),);
}


The second piece of this puzzle is in the block's auto.js file. This javascript file performs the actual validation of the form field, and pops up the alert with the error message if the data entered is invalid. Below is an example of the auto.js file in my block

function ccmValidateBlockForm() 
{
    var failed=0;
    if ($('#slideDuration').val() == '' || $('#slideDuration').val() == null)
    {
        alert(ccm_t('slideDuration-required'));
        failed=1;
    }
    if(failed)
    {
        ccm_isBlockError=1;
        return false;
    }
    return true;
}


The problem I originally had with my block, and that was causing the undefined alert, was that the 'slideDuration-required' string in auto.js alert did not match the same string in the getJavaScriptStrings() function in the controller. ie in the auto.js I had actually typed 'slideInterval-required' instead of 'slideDuration-required'. Once I corrected this, the undefined alert no longer occurred.

Anyway, hopefully you, or someone else, might find this solution useful in the future

Regards

James
jordanlev replied on at Permalink Reply
jordanlev
If you are building a custom block for yourself (not for marketplace distribution) and only need to support one language in your back-end, you can greatly simplify the validation by not having the "getJavascriptStrings" function in your controller. Then in your auto.js file, just pass a string to the alert() function (instead of the "ccm_t()" function).

In your example, you would replace this:
alert(ccm_t('slideDuration-required'));

...with this:
alert('Please enter a numeric Slide Duration. This controls for how long ... blah bah blah');


The "getJavascriptStrings" function (and its corresponding "ccm_t" function) exist only to support multiple languages. If you don't need multiple languages and aren't releasing this as a public addon, then there's no need to add the extra code and hence you can avoid problems with mis-matched string handles.