<commit_before>/**
 * This file is a copy of template JsFormValidationBundle::JsFormValidation.js.twig.
 *
 * File was copied, not extended because function getComputeMessage in original template can return
 * "undefined" in some cases, but unfortunatelly it can be overriden using twig extend (see ).
 * Task for complete refactoring: BAP-859
 */

require(['jquery', 'underscore', 'oro/translator', 'jquery.bind-first', 'bootstrap-tooltip'],
function($, _, __){
{% block before %}{% endblock %}

var jsfv = (function () {
    function getComputeMessage(key, placeholders, number) {
        var result = __('validators:'+key, placeholders, number);
        if (undefined == result && number != undefined) {
            // Try to get translated string without a number parameter.
            result = __('validators:'+key, placeholders);
        }
        return result;
    }

    function isNotDefined(value) {
        return (typeof(value) == 'undefined' || null === value || '' === value);
    }

{% block getFieldValue %}
    function getFieldValue(field) {
        if (!field || !field.nodeName) {
            return undefined;
        }

        var resolveField = function(field) {
            if (-1 != $.inArray(field.nodeName.toLowerCase(), ['input', 'select', 'textarea'])) {
                return $(field);
            }
            var select = $('select', field);
            if (select.length) {
                return select;
            }
            var input = $('input', field);
            if (input.length) {
                if (!input.filter('[type=checkbox]') && !input.filter('[type=radio]')) {
                    return input.first();
                }
                return input;
            }
            var textarea = $('textarea', field);
                if (textarea.length) {
                return textarea.get(0);
            }
            return undefined;
        }

        var $field = resolveField(field);
        var isMultiple = $field.length > 1 || $field.filter('select[multiple=multiple]').length;
        var value = undefined;

        if ($field.filter('input[type="checkbox"]').length) {
            value = $field.filter(':checked').map(function(i, cb) { return $(cb).val(); }).get();
        } else if ($field.filter('input[type="radio"]').length) {
            value = $field.filter(':checked').val();
        } else {
            value = $field.val();
        }

        if (!isMultiple && value instanceof Array) {
            value = value.pop();
        } else if (isMultiple && !value instanceof Array) {
            if (value && value != '') {
                value = [value];
            } else {
                value = [];
            }
        }

        return value;
    }
{% endblock %}

    function checkError(field, checkFunction, parameters, value) {
        if (!$("#" + field).length || $("#" + field).is(':disabled')) {
            return true;
        }
        field = jsfv.id(field);
        // Remove old errors of the field
        {% block removeErrors %}{% spaceless %}jsfv.removeErrors(field);{% endspaceless %}{% endblock %}
        // Check the value
        errorMessage = checkFunction((value === undefined ? getFieldValue(field) : value), parameters);
        {# Notice! Block getVal was refused since 2.1 version.
           Use twig extention function getJsFormElementValue('field') instead. -#}
        /*// {% block getVal %}{% endblock %} */

        if (errorMessage != true) {
{% block addError %}
        {% spaceless %}jsfv.addError(field, errorMessage);{% endspaceless %}
{% endblock %}

            return false;
        }

        return true;
    }


{% for librairyCall in librairyCalls %}

{% include librairyCall %}

{% endfor %}

{% for handler in getterHandlers %}

{{ handler|raw }}

{% endfor %}

    return {
        id: function (id) {
            return document.getElementById(id);
        },
{% block removeErrors_method %}
        removeErrors: function (field) {
            field = $(field);
            if (field.parent().filter('.error').length) {
                field = field.parent();
            }
            field.siblings('.validation-faled').remove();
            field.removeClass('error').parent().filter('.controls').removeClass('validation-error');
        },
{% endblock %}
{% block addError_method %}
        addError: function (field, errorMessage) {
            // Add errors block
            field = $(field);
            var target = field;
            if (field.parent().filter('.selector').length || field.parent().filter('.uploader').length) {
                target = field.parent();
            }
            if (!target.siblings('.validation-faled').length) {
                $('<span class="validation-faled"></span>').insertAfter(target);
            }
            target.siblings('.validation-faled').text(errorMessage);
            target.addClass('error').parent().filter('.controls').addClass('validation-error');
        },
{% endblock %}
        onEvent: function (field, eventType, handler) {
            if (typeof(field) == 'string') {
                field = jsfv.id(field);
            }
{% block onEvent %}
            $(field).bind(eventType, handler);
{% endblock %}
        },
        onFirstEvent: function (field, eventType, handler) {
            if (typeof(field) == 'string') {
                field = jsfv.id(field);
            }
{% block onFirstEvent %}
            $(field).bindFirst(eventType, handler);
{% endblock %}
        },
{% for fieldName, constraints in fieldConstraints %}
        check_{{ fieldName }}: function() {
            var gv;
            result = true;
{% for constraint in constraints %}
            result = result && checkError('{{   fieldName }}', {{ constraint.name }}, {{ constraint.parameters|raw }} );
{% endfor %}
{% set resultvar = 'result' %}
{% block getters_constraints_field %}
{% if gettersConstraints[fieldName] is defined %}
{% for getterHandler, getterConstraints  in gettersConstraints[fieldName] %}
            if ({{ resultvar }}) {
                gv = {{ getterHandler|raw }}();
{% for constraint in getterConstraints %}
                {{ resultvar }} = checkError({% if fieldName == '.' %}form.id{% else %}'{{ fieldName }}'{% endif %}, {{ constraint.name }}, {{ constraint.parameters|raw }}, gv);
{% endfor %}
            }
{% endfor %}
{% endif %}
{% endblock %}
            return result;
        },
{% endfor %}
        onReady: function() {
{% if check_modes.submit %}
            // On submit checks
            var form = jsfv.id('{{ formName }}');

            // Form exists ?
            if (form) {
                // Get the form
                if ( form.nodeName.toLowerCase() != 'form') {
                    form = jsfv.id('{{ formName }}__token').form;
                }

                form = $(form)
                // Remove HTML5 validation
                form.attr('novalidate', 'novalidate')
                // autofocus on a first form input
                form.find(':input:visible:not(.offscreen):not(button):not([disabled]):not([readonly])')
                    .first().focus();

                jsfv.onFirstEvent(form, 'submit', function(e) {
                    var gv, submitForm = true;
{% for fieldName, constraints in fieldConstraints %}
                    submitForm = jsfv.check_{{ fieldName }}() && submitForm;
{% endfor %}
{% set resultvar = 'submitForm' %}{% set fieldName = '.' %}
{% block getters_constraints_submit %}{{ block('getters_constraints_field') }}{% endblock %}
                    if (!submitForm) {
                        form.find('.error>:input,:input.error').first().focus();
                        e.stopImmediatePropagation();
                    }
                    return submitForm;
                });
            }
{% endif %}

{% if check_modes.blur %}
            // On blur checks
{% for fieldName, constraints in fieldConstraints %}
            jsfv.onEvent('{{ fieldName }}', 'blur', jsfv.check_{{ fieldName }});
{% endfor %}
{% endif %}

{% if check_modes.change %}
            // On change checks
{% for fieldName, constraints in fieldConstraints %}
            jsfv.onEvent('{{ fieldName }}', 'change', jsfv.check_{{ fieldName }});
{% endfor %}
{% endif %}
        }
    };
}());

{% block onReady %}
$(_.debounce(jsfv.onReady, 50));
{% endblock %}

{% block after %}{% endblock %}
});<commit_msg>Fixed BAP-1673: Redesign validation messages<commit_after>/**
 * This file is a copy of template JsFormValidationBundle::JsFormValidation.js.twig.
 *
 * File was copied, not extended because function getComputeMessage in original template can return
 * "undefined" in some cases, but unfortunatelly it can be overriden using twig extend (see ).
 * Task for complete refactoring: BAP-859
 */

require(['jquery', 'underscore', 'oro/translator', 'jquery.bind-first', 'bootstrap-tooltip'],
function($, _, __){
{% block before %}{% endblock %}

var jsfv = (function () {
    function getComputeMessage(key, placeholders, number) {
        var result = __('validators:'+key, placeholders, number);
        if (undefined == result && number != undefined) {
            // Try to get translated string without a number parameter.
            result = __('validators:'+key, placeholders);
        }
        return result;
    }

    function isNotDefined(value) {
        return (typeof(value) == 'undefined' || null === value || '' === value);
    }

{% block getFieldValue %}
    function getFieldValue(field) {
        if (!field || !field.nodeName) {
            return undefined;
        }

        var resolveField = function(field) {
            if (-1 != $.inArray(field.nodeName.toLowerCase(), ['input', 'select', 'textarea'])) {
                return $(field);
            }
            var select = $('select', field);
            if (select.length) {
                return select;
            }
            var input = $('input', field);
            if (input.length) {
                if (!input.filter('[type=checkbox]') && !input.filter('[type=radio]')) {
                    return input.first();
                }
                return input;
            }
            var textarea = $('textarea', field);
                if (textarea.length) {
                return textarea.get(0);
            }
            return undefined;
        }

        var $field = resolveField(field);
        var isMultiple = $field.length > 1 || $field.filter('select[multiple=multiple]').length;
        var value = undefined;

        if ($field.filter('input[type="checkbox"]').length) {
            value = $field.filter(':checked').map(function(i, cb) { return $(cb).val(); }).get();
        } else if ($field.filter('input[type="radio"]').length) {
            value = $field.filter(':checked').val();
        } else {
            value = $field.val();
        }

        if (!isMultiple && value instanceof Array) {
            value = value.pop();
        } else if (isMultiple && !value instanceof Array) {
            if (value && value != '') {
                value = [value];
            } else {
                value = [];
            }
        }

        return value;
    }
{% endblock %}

    function checkError(field, checkFunction, parameters, value) {
        if (!$("#" + field).length || $("#" + field).is(':disabled')) {
            return true;
        }
        field = jsfv.id(field);
        // Remove old errors of the field
        {% block removeErrors %}{% spaceless %}jsfv.removeErrors(field);{% endspaceless %}{% endblock %}
        // Check the value
        errorMessage = checkFunction((value === undefined ? getFieldValue(field) : value), parameters);
        {# Notice! Block getVal was refused since 2.1 version.
           Use twig extention function getJsFormElementValue('field') instead. -#}
        /*// {% block getVal %}{% endblock %} */

        if (errorMessage != true) {
{% block addError %}
        {% spaceless %}jsfv.addError(field, errorMessage);{% endspaceless %}
{% endblock %}

            return false;
        }

        return true;
    }


{% for librairyCall in librairyCalls %}

{% include librairyCall %}

{% endfor %}

{% for handler in getterHandlers %}

{{ handler|raw }}

{% endfor %}

    return {
        id: function (id) {
            return document.getElementById(id);
        },
{% block removeErrors_method %}
        removeErrors: function (field) {
            field = $(field);
            if (field.parent().filter('.error').length) {
                field = field.parent();
            }
            field.siblings('.validation-faled').remove();
            field.removeClass('error').parent().filter('.controls').removeClass('validation-error');
        },
{% endblock %}
{% block addError_method %}
        addError: function (field, errorMessage) {
            // Add errors block
            field = $(field);
            var target = field;
            if (field.parent().filter('.selector').length || field.parent().filter('.uploader').length) {
                target = field.parent();
            }
            if (!target.siblings('.validation-faled').length) {
                $('<span class="validation-faled"></span>').insertAfter(target);
            }
            target.siblings('.validation-faled').text(errorMessage);
            target.addClass('error').parent().filter('.controls').addClass('validation-error');
        },
{% endblock %}
        onEvent: function (field, eventType, handler) {
            if (typeof(field) == 'string') {
                field = jsfv.id(field);
            }
{% block onEvent %}
            $(field).bind(eventType, handler);
{% endblock %}
        },
        onFirstEvent: function (field, eventType, handler) {
            if (typeof(field) == 'string') {
                field = jsfv.id(field);
            }
{% block onFirstEvent %}
            $(field).bindFirst(eventType, handler);
{% endblock %}
        },
{% for fieldName, constraints in fieldConstraints %}
        check_{{ fieldName }}: function() {
            var gv;
            result = true;
{% for constraint in constraints %}
            result = result && checkError('{{   fieldName }}', {{ constraint.name }}, {{ constraint.parameters|raw }} );
{% endfor %}
{% set resultvar = 'result' %}
{% block getters_constraints_field %}
{% if gettersConstraints[fieldName] is defined %}
{% for getterHandler, getterConstraints  in gettersConstraints[fieldName] %}
            if ({{ resultvar }}) {
                gv = {{ getterHandler|raw }}();
{% for constraint in getterConstraints %}
                {{ resultvar }} = checkError({% if fieldName == '.' %}form.id{% else %}'{{ fieldName }}'{% endif %}, {{ constraint.name }}, {{ constraint.parameters|raw }}, gv);
{% endfor %}
            }
{% endfor %}
{% endif %}
{% endblock %}
            return result;
        },
{% endfor %}
        onReady: function() {
{% if check_modes.submit %}
            // On submit checks
            var form = jsfv.id('{{ formName }}');

            // Form exists ?
            if (form) {
                // Get the form
                if ( form.nodeName.toLowerCase() != 'form') {
                    form = jsfv.id('{{ formName }}__token').form;
                }

                form = $(form);
                // Remove HTML5 validation
                form.attr('novalidate', 'novalidate');
                // autofocus on a first form input
                form.find(':input:visible:not(.offscreen):not(button):not([disabled]):not([readonly])')
                    .first().focus();

                jsfv.onFirstEvent(form, 'submit', function(e) {
                    var gv, submitForm = true;
{% for fieldName, constraints in fieldConstraints %}
                    submitForm = jsfv.check_{{ fieldName }}() && submitForm;
{% endfor %}
{% set resultvar = 'submitForm' %}{% set fieldName = '.' %}
{% block getters_constraints_submit %}{{ block('getters_constraints_field') }}{% endblock %}
                    if (!submitForm) {
                        form.find('.error>:input,:input.error').first().focus();
                        e.stopImmediatePropagation();
                    }
                    return submitForm;
                });
            }
{% endif %}

{% if check_modes.blur %}
            // On blur checks
{% for fieldName, constraints in fieldConstraints %}
            jsfv.onEvent('{{ fieldName }}', 'blur select2-blur', jsfv.check_{{ fieldName }});
{% endfor %}
{% endif %}

{% if check_modes.change %}
            // On change checks
{% for fieldName, constraints in fieldConstraints %}
            jsfv.onEvent('{{ fieldName }}', 'change', jsfv.check_{{ fieldName }});
{% endfor %}
{% endif %}
        }
    };
}());

{% block onReady %}
$(_.debounce(jsfv.onReady, 50));
{% endblock %}

{% block after %}{% endblock %}
});
{'fraction_non_alphanumeric': 0.1571411596958175, 'fraction_numerical': 0.001604087452471483, 'mean_word_length': 2.0101931330472103, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3405955', 'n_tokens_mistral': 4815, 'n_tokens_neox': 4427, 'n_words': 1047}