Parsley: Customize error message

Asked

Viewed 190 times

1

I’m using the plugin Parsley for form validation and I am creating some additional validation methods, however, I am having difficulty with the customization of the error messages of these functions.

According to the site documentation, we can add a custom method as follows:

window.Parsley.addValidator('equals', {
    validateString: function validateString(value, comparar_com, texto_erro) {
        return (value === comparar_com);
    },
    requirementType: 'string',
    messages: {
        pt_BR: 'O seguinte valor é esperado: %s'
    }
});

With the above code Parsley would display in the error message the second parameter of the function, which would be 'compare with', but I wanted the text defined in the third parameter, 'text_error', to be displayed in the error message, and not 'compare with''.

Can anyone tell me if this is possible? Parsley uses the following function to mount the error messages:

// Kind of light `sprintf()` implementation
formatMessage: function formatMessage(string, parameters) {
...

I’m trying something here, still unsuccessful, but am I on the right track?

  • Yesterday looking at some links on the internet I realized the following, the function validaeString (or any other variation) receives up to three parameters: value, requirement e fieldInstance. I mean, the way I was imagining... =(

1 answer

0

Just to keep it logged as customizing an error message using one of the parameters used in the validation as part of the message, see below an example of a function I added to Parsley:

/**
 * Verifica se o valor existe em uma lista de valores permitidos
 *
 * @param   string    value   Valor que será validado
 * @param   mix       list    Lista de valores permitidos. Pode ser um array ou uma string separada por vírgula
 * 
 * @return  bool
 *
 * Utilização:
 * data-parsley-in-list="Opção 1,Opção 2"
 *
 */
window.Parsley.addValidator('inList',
{
    validateString: function validateString(value, list)
    {
        if(typeof list != 'object')
        {
            list = list.split(',');
        }
        if(list.length > 0)
        {
            for(var i in list)
            {
                // trim
                if(value === list[i].replace(/^\s+|\s+$/gm, ''))
                {
                    return true;
                }
            }
        }

        // Monta a string com a lista das opções
        var new_list = list.join(', ');

        // Redefine a mensagem de erro formatando a lista de opções
        var new_error_msg = 'O valor deve ser uma das seguintes opções: ' + new_list;

        // Atualiza a mensagem do erro
        window.Parsley._validatorRegistry.catalog.pt_BR.inList = new_error_msg;

        return false;
    },
    requirementType: 'string',
    messages:
    {
        pt_BR: 'O valor deve ser uma das seguintes opções: %s'
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.