solved Javascript Form Message

TiG
TiG
@tig
4 years ago
184 posts
I have a need to set messages in a form via a Javascript function. While it is easy enough to directly address the message area using the id of
id="{$form_name}_msg"
this seems like a hack.

I cannot seem to find a suitable Jamroom javascript function that will set/clear the message. Does one exist? If not, what would be a recommended future-proof method to set the form message via javascript? (Note: there is no server interaction here.)


--
TiG

updated by @tig: 07/14/20 04:26:03AM
michael
@michael
4 years ago
7,714 posts
If you're talking about in a form then you can use the 'error_msg' parameter to define your own custom message, eg:
    $_tmp = array(
        'name'      => 'profile_id',
        'label'     => 'profile name',
        'type'      => 'live_search',
        'help'      => 'Select the profile to reset the business settings for',
        'validate'  => 'not_empty',
        'required'  => true,
        'error_msg' => 'You have selected an invalid Profile - please try again',
        'target'    => jrCore_get_base_url() . "/{$_post['module_url']}/search_business_profiles"
    );
    jrCore_form_field_create($_tmp);

if you're talking about an alert style thing then:
jrCore_alert('this is an alert message, but pretty')

You've said JAVASCRIPT so you probably dont want
// set a notice in php
 jrCore_set_form_notice('error', 'invalid something');
// get any notices that were set
 jrCore_get_form_notice();
any of that useful, im not sure.
TiG
TiG
@tig
4 years ago
184 posts
Michael

Here is what I created in lieu of an existing function:

/**
 * Set the contents of the argument form message_id to message as an error or success
 * @param {string}  message_id - the HTML id of the element containing the message
 * @param {string}  message - the HTML to include as the message
 * @param {boolean} error - if true, mark this as an error message
 */
function ntCore_set_form_message(message_id, message, error){
    var selector = '#'+message_id;

    if ($(selector).length)                                             // ensure message_id element exists
    {
        if (typeof error === 'undefined' || error == false)
                $(selector).html(message).removeClass('error').show();
        else $(selector).html(message).addClass('error').show();
    }
}

This of course works fine, but it is based on knowing that the 'error' class is used for severity and the caller must know the exact HTML id of the form element that contains the message. What I would prefer to use is a function like jrFormMessages() but nobody seems to use it and it does not appear to work. Ideally the Javascript client simply calls a function to display a message on the designated message area of a form but is not bound to the exact ID and specific class behavior.

What I have works fine, but I always like to check to see if there is a more future-proof method in cases like this.

Thanks,
TiG


--
TiG
michael
@michael
4 years ago
7,714 posts
You could use the .form_submit_box element and add your own div in there
function xxSomething_show_error_message(){
    $('.my_error_message').remove();
    $('.form_submit_box').prepend('<div class="my_error_message page_notice error">Some message before the SUBMIT button</div>');
}

Usually there will only be one form on a form page and only one submit button. This way would run into difficulties if there were more than one form on the page, only the first form would get the message.

If its on the skin side of things then thats open to customization so harder to have a fixed setup.

I don't think there is anything specific setup as a do-it-this-way guide on this subject.
TiG
TiG
@tig
4 years ago
184 posts
Michael

All good. That is the info I was asking for. I just do not want to reinvent the wheel (of course).

As always, much appreciated.

TiG


--
TiG

Tags