<commit_before>const localize              = require('../base/localize').localize;
const compareBigUnsignedInt = require('../common_functions/string_util').compareBigUnsignedInt;

const Validation = (() => {
    'use strict';

    const forms = {};
    const error_class  = 'error-msg';
    const hidden_class = 'invisible';

    const events_map = {
        input   : 'input change',
        select  : 'change',
        checkbox: 'change',
    };

    const getFieldType = $field => (
        $field.length ? ($field.attr('type') === 'checkbox' ? 'checkbox' : $field.get(0).localName) : null
    );

    const getFieldValue = field => (field.type === 'checkbox' ? (field.$.is(':checked') ? '1' : '') : field.$.val()) || '';

    const initForm = (form_selector, fields) => {
        const $form = $(`${form_selector}:visible`);
        if ($form.length) {
            forms[form_selector] = { $form: $form };
            if (Array.isArray(fields) && fields.length) {
                forms[form_selector].fields = fields;
                const $btn_submit = $form.find('button[type="submit"]');

                fields.forEach((field) => {
                    field.$ = $form.find(field.selector);
                    if (!field.$.length || !field.validations) return;

                    field.type = getFieldType($(field.$[0])); // also handles multiple results
                    field.form = form_selector;
                    if (field.msg_element) {
                        field.$error = $form.find(field.msg_element);
                    } else {
                        const $parent = field.$.parent();
                        // Add indicator to required fields
                        if (/req/.test(field.validations)) {
                            let $label = $parent.parent().find('label');
                            if (!$label.length) $label = $parent.find('label');
                            if ($label.length && $label.find('span.required_field_asterisk').length === 0) {
                                $label.append($('<span/>', { class: 'required_field_asterisk', text: '*' }));
                                if ($form.find('.required_field_asterisk.no-margin').length === 0) {
                                    $btn_submit.parent().append($('<p/>', { class: 'hint' })
                                        .append($('<span/>', { class: 'required_field_asterisk no-margin', text: '*' })).append($('<span/>', { text: ` ${localize('Indicates required field')}` })));
                                }
                            }
                        }
                        if ($parent.find(`div.${error_class}`).length === 0) {
                            $parent.append($('<div/>', { class: `${error_class} ${hidden_class}` }));
                        }
                        field.$error = $parent.find(`.${error_class}`);
                    }

                    const event = events_map[field.type];
                    if (event) {
                        field.$.unbind(event).on(event, () => {
                            checkField(field);
                            if (field.re_check_field) {
                                checkField(forms[form_selector].fields.find(fld => (
                                    fld.selector === field.re_check_field
                                )));
                            }
                        });
                    }
                });<commit_msg>only add required to the first label<commit_after>const localize              = require('../base/localize').localize;
const compareBigUnsignedInt = require('../common_functions/string_util').compareBigUnsignedInt;

const Validation = (() => {
    'use strict';

    const forms = {};
    const error_class  = 'error-msg';
    const hidden_class = 'invisible';

    const events_map = {
        input   : 'input change',
        select  : 'change',
        checkbox: 'change',
    };

    const getFieldType = $field => (
        $field.length ? ($field.attr('type') === 'checkbox' ? 'checkbox' : $field.get(0).localName) : null
    );

    const getFieldValue = field => (field.type === 'checkbox' ? (field.$.is(':checked') ? '1' : '') : field.$.val()) || '';

    const initForm = (form_selector, fields) => {
        const $form = $(`${form_selector}:visible`);
        if ($form.length) {
            forms[form_selector] = { $form: $form };
            if (Array.isArray(fields) && fields.length) {
                forms[form_selector].fields = fields;
                const $btn_submit = $form.find('button[type="submit"]');

                fields.forEach((field) => {
                    field.$ = $form.find(field.selector);
                    if (!field.$.length || !field.validations) return;

                    field.type = getFieldType($(field.$[0])); // also handles multiple results
                    field.form = form_selector;
                    if (field.msg_element) {
                        field.$error = $form.find(field.msg_element);
                    } else {
                        const $parent = field.$.parent();
                        // Add indicator to required fields
                        if (/req/.test(field.validations)) {
                            let $label = $parent.parent().find('label');
                            if (!$label.length) $label = $parent.find('label');
                            if ($label.length && $label.find('span.required_field_asterisk').length === 0) {
                                $($label[0]).append($('<span/>', { class: 'required_field_asterisk', text: '*' }));
                                if ($form.find('.required_field_asterisk.no-margin').length === 0) {
                                    $btn_submit.parent().append($('<p/>', { class: 'hint' })
                                        .append($('<span/>', { class: 'required_field_asterisk no-margin', text: '*' })).append($('<span/>', { text: ` ${localize('Indicates required field')}` })));
                                }
                            }
                        }
                        if ($parent.find(`div.${error_class}`).length === 0) {
                            $parent.append($('<div/>', { class: `${error_class} ${hidden_class}` }));
                        }
                        field.$error = $parent.find(`.${error_class}`);
                    }

                    const event = events_map[field.type];
                    if (event) {
                        field.$.unbind(event).on(event, () => {
                            checkField(field);
                            if (field.re_check_field) {
                                checkField(forms[form_selector].fields.find(fld => (
                                    fld.selector === field.re_check_field
                                )));
                            }
                        });
                    }
                });
{'fraction_non_alphanumeric': 0.15713668443036152, 'fraction_numerical': 0.0018723894570070575, 'mean_word_length': 1.351506942092787, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7388920', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1713, 'n_words': 298}