victorjonsson / jQuery-Form-Validator

[DISCONTINUED] jQuery plugin that makes it easy to validate user input while keeping your HTML markup clean from javascript code.
972 stars 476 forks source link

Send "eventContext" as parameter to "validatorFunction" #489

Open jbustamanterendon opened 8 years ago

jbustamanterendon commented 8 years ago

Hello,

We need a new feature on the "validatorFunction" need to receive the "eventContext" as the last parameter, thank you.

Best regards.

victorjonsson commented 8 years ago

Yes, that sounds reasonable.

The concept behind the "eventContext" was not really thought through when introduced, but since it has stayed around for some time now it probably exists for a good reason.

jbustamanterendon commented 8 years ago

Hello,

In our case it is necessary to know if it was launched by "Blur" or "Submit", based on this we take different decisions.

check this code:

$.formUtils.addValidator({
    name: 'isVatNumber',
    validatorFunction: function (value, $el, config, language, $form, eventContext) {
        var result = true;

        var data = {
            url_call: orderOpcUrl + '?rand=' + new Date().getTime(),
            is_ajax: true,
            async: false,
            action: 'checkVATNumber',
            vat_number: value
        };

        //if (!$.isEmpty(value)){
            var _json = {
                data: data,
                beforeSend: function(){
                    $('div#onepagecheckoutps #onepagecheckoutps_step_one_container .loading_small').show();
                },
                success: function(data) {
                    $('div#onepagecheckoutps #onepagecheckoutps_step_one_container .loading_small').hide();

                    if (!$.isEmpty(data[0])){
                        Fronted.showModal({type:'error', message : data[0]});
                        result = false;
                    } else {
                        if (eventContext == 'blur') {
                            Carrier.getByCountry();
                        }
                    }
                }
            };
            $.makeRequest(_json);
        //}

        return result;
    },
    validateOnKeyUp: false,
    errorMessage: 'This is not a valid.',
    errorMessageKey: 'errorGlobal'});

Best regards.

victorjonsson commented 8 years ago

But your code doesn't make sense to me. Isn't $.makeRequest asynchronous? That means that this validator will always return result = true.

The validatorFunction does not support async behaviour.

jbustamanterendon commented 8 years ago

I just like to show you this:

if (eventContext == 'blur') {
Carrier.getByCountry();
}

On my validations custom fields, need the "eventContext" :)

victorjonsson commented 8 years ago

Fixed!

jbustamanterendon commented 8 years ago

Great!! very thanks!. This plugins is the better !! We use it in our project module for PrestaShop http://www.presteamshop.com/en/modules-prestashop/one-page-checkout-prestashop.html

victorjonsson commented 8 years ago

We'll have it open until stable version the plugin is released