|
1906 | 1906 | * @protected
|
1907 | 1907 | */
|
1908 | 1908 | _create: function () {
|
| 1909 | + this._prepareArrayInputs(); |
1909 | 1910 | this.validate = this.element.validate(this.options);
|
1910 | 1911 |
|
1911 | 1912 | // ARIA (adding aria-required attribute)
|
|
1918 | 1919 | this._listenFormValidate();
|
1919 | 1920 | },
|
1920 | 1921 |
|
| 1922 | + /** |
| 1923 | + * Validation creation. |
| 1924 | + * |
| 1925 | + * @protected |
| 1926 | + */ |
| 1927 | + _prepareArrayInputs: function () { |
| 1928 | + /* Store original names for array inputs */ |
| 1929 | + var originalElements = [], |
| 1930 | + originalSubmitHandler = this.options.submitHandler; |
| 1931 | + |
| 1932 | + /* For all array inputs, assign index so that validation is proper */ |
| 1933 | + this.element.find('[name$="[]"]').each(function (key, input) { |
| 1934 | + var originalName, name; |
| 1935 | + |
| 1936 | + input = $(input); |
| 1937 | + originalName = input.attr('name'); |
| 1938 | + name = originalName.replace('[]', '[' + key + ']'); |
| 1939 | + $(input).attr('name', name); |
| 1940 | + $(input).attr('orig-name', originalName); |
| 1941 | + originalElements.push({ |
| 1942 | + element: $(input), |
| 1943 | + name: originalName |
| 1944 | + }); |
| 1945 | + }); |
| 1946 | + |
| 1947 | + if (originalElements.length) { |
| 1948 | + /** |
| 1949 | + * Before submitting the actual form, remove the previously assigned indices |
| 1950 | + * @param {Object} form |
| 1951 | + */ |
| 1952 | + this.options.submitHandler = function (form, event) { |
| 1953 | + originalElements.forEach(function (element) { |
| 1954 | + element.element.attr('name', element.name); |
| 1955 | + element.element.removeAttr('orig-name'); |
| 1956 | + }); |
| 1957 | + |
| 1958 | + console.error(this.submit) |
| 1959 | + /* Call the originalSubmitHandler if it's a function */ |
| 1960 | + typeof originalSubmitHandler === 'function' ? originalSubmitHandler(form) : form.submit(); |
| 1961 | + }; |
| 1962 | + } |
| 1963 | + }, |
| 1964 | + |
1921 | 1965 | /**
|
1922 | 1966 | * Validation listening.
|
1923 | 1967 | * @protected
|
|
0 commit comments