N
N
Nikolai Vinogradov2017-02-09 13:23:10
JavaScript
Nikolai Vinogradov, 2017-02-09 13:23:10

Why is this.init is not a function when running validation in a react app?

There is a react form, the validation of which goes through the jquery validation plugin. I validate in the following way:

let validator = $('form').validate({
            errorClass:  'has-error',
            errorElement:'label',
        });
        validator.form();
        if ($('.has-error').length > 0) {
            $('.has-error').each(function (index) {
                $.validator().showErrors({prop:$(this).data('error')});
            });
        } else {
 /*обрабатываем данные*/}

Errors all that is necessary displays, but stops and issues to the console
this.init is not a function

Swears at this place in the plugin script:
$.validator = function( options, form ) {
    this.settings = $.extend( true, {}, $.validator.defaults, options );
    this.currentForm = form;
    this.init();
  };

How to be, how to fix?
UPD: I found this in the plugin script (this piece goes lower than the one referred to by the error):
$.extend( $.validator, {
//some code
prototype: {

        init: function() {
            this.labelContainer = $( this.settings.errorLabelContainer );
            this.errorContext = this.labelContainer.length && this.labelContainer || $( this.currentForm );
            this.containers = $( this.settings.errorContainer ).add( this.settings.errorLabelContainer );
            this.submitted = {};
            this.valueCache = {};
            this.pendingRequest = 0;
            this.pending = {};
            this.invalid = {};
            this.reset();
//some code

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolai Vinogradov, 2017-02-09
@Kibastus

The error was that $.validator() is essentially a constructor, and so an already created validator instance should have been used instead in the error line.

E
emp1re, 2017-02-09
@emp1re

prop:$(this) === .bind(this)? Try to convey the context yourself. Obviously there is no reference to this function in this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question