|
| 1 | +QUnit.module('Validate SimpleForm Bootstrap 4 Datetime Fields/Wrappers', { |
| 2 | + before: function () { |
| 3 | + currentFormBuilder = window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] |
| 4 | + window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = BS4_FORM_BUILDER |
| 5 | + }, |
| 6 | + |
| 7 | + after: function () { |
| 8 | + window.ClientSideValidations.formBuilders['SimpleForm::FormBuilder'] = currentFormBuilder |
| 9 | + }, |
| 10 | + |
| 11 | + beforeEach: function () { |
| 12 | + dataCsv = { |
| 13 | + html_settings: { |
| 14 | + type: 'SimpleForm::FormBuilder', |
| 15 | + error_class: null, |
| 16 | + error_tag: 'div', |
| 17 | + wrapper_error_class: 'form-group-invalid', |
| 18 | + wrapper_tag: 'div', |
| 19 | + wrapper_class: 'form-group' |
| 20 | + }, |
| 21 | + validators: { |
| 22 | + 'user[date_of_birth]': { presence: [{ message: 'must be present' }] }, |
| 23 | + 'user[time_of_birth]': { presence: [{ message: 'must be present' }] } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + $('#qunit-fixture') |
| 28 | + .append( |
| 29 | + $('<form>', { |
| 30 | + action: '/users', |
| 31 | + 'data-client-side-validations': JSON.stringify(dataCsv), |
| 32 | + method: 'post', |
| 33 | + id: 'new_user' |
| 34 | + }) |
| 35 | + .append($('<div>', { |
| 36 | + class:'form-group date required user_date_of_birth' |
| 37 | + }) |
| 38 | + .append($('<label class="form-control-label date required" for="user_date_of_birth_1i">Date of birth <abbr title="required">*</abbr></label>')) |
| 39 | + .append($('<div>', { |
| 40 | + class: 'd-flex flex-row justify-content-between align-items-center' |
| 41 | + }) |
| 42 | + .append($('<select>', { |
| 43 | + id: 'user_date_of_birth_1i', |
| 44 | + name: 'user[date_of_birth(1i)]', |
| 45 | + class: 'form-control mx-1 date required', |
| 46 | + 'data-client-side-validations-wrapper': 'vertical_multi_select' |
| 47 | + }) |
| 48 | + .append($('<option value=""></option>')) |
| 49 | + .append($('<option value="2015">2015</option>')) |
| 50 | + .append($('<option value="2025">2025</option>'))) |
| 51 | + .append($('<select>', { |
| 52 | + id: 'user_date_of_birth_2i', |
| 53 | + name: 'user[date_of_birth(2i)]', |
| 54 | + class: 'form-control mx-1 date required', |
| 55 | + 'data-client-side-validations-wrapper': 'vertical_multi_select' |
| 56 | + }) |
| 57 | + .append($('<option value=""></option>')) |
| 58 | + .append($('<option value="1">January</option>')) |
| 59 | + .append($('<option value="2">February</option>'))) |
| 60 | + .append($('<select>', { |
| 61 | + id: 'user_date_of_birth_3i', |
| 62 | + name: 'user[date_of_birth(3i)]', |
| 63 | + class: 'form-control mx-1 date required', |
| 64 | + 'data-client-side-validations-wrapper': 'vertical_multi_select' |
| 65 | + }) |
| 66 | + .append($('<option value=""></option>')) |
| 67 | + .append($('<option value="1">1</option>')) |
| 68 | + .append($('<option value="2">2</option>')) |
| 69 | + .append($('<option value="3">3</option>'))) |
| 70 | + ) |
| 71 | + .append('<small class="form-text text-muted">hint text</small>') |
| 72 | + ) |
| 73 | + .append($('<div>', { |
| 74 | + class:'form-group time required user_time_of_birth form-group-invalid' |
| 75 | + }) |
| 76 | + .append($('<label class="form-control-label time required" for="user_time_of_birth_4i">Time of birth <abbr title="required">*</abbr></label>')) |
| 77 | + .append($('<div>', { |
| 78 | + class: 'd-flex flex-row justify-content-between align-items-center' |
| 79 | + }) |
| 80 | + .append('<input type="hidden" id="user_time_of_birth_1i" name="user[time_of_birth(1i)]" value="1">') |
| 81 | + .append('<input type="hidden" id="user_time_of_birth_2i" name="user[time_of_birth(2i)]" value="1">') |
| 82 | + .append('<input type="hidden" id="user_time_of_birth_3i" name="user[time_of_birth(3i)]" value="1">') |
| 83 | + .append($('<select>', { |
| 84 | + id: 'user_time_of_birth_4i', |
| 85 | + name: 'user[time_of_birth(4i)]', |
| 86 | + class: 'form-control mx-1 is-invalid time required', |
| 87 | + 'data-client-side-validations-wrapper': 'vertical_multi_select' |
| 88 | + }) |
| 89 | + .append($('<option value=""></option>')) |
| 90 | + .append($('<option value="00">00</option>')) |
| 91 | + .append($('<option value="23">23</option>')) |
| 92 | + ) |
| 93 | + .append(':') |
| 94 | + .append($('<select>', { |
| 95 | + id: 'user_time_of_birth_5i', |
| 96 | + name: 'user[time_of_birth(5i)]', |
| 97 | + class: 'form-control mx-1 is-invalid time required', |
| 98 | + 'data-client-side-validations-wrapper': 'vertical_multi_select' |
| 99 | + }) |
| 100 | + .append($('<option value=""></option>')) |
| 101 | + .append($('<option value="00">00</option>')) |
| 102 | + .append($('<option value="55">55</option>')) |
| 103 | + ) |
| 104 | + ) |
| 105 | + .append('<div class="invalid-feedback d-block">Time of birth must be present.</div>') |
| 106 | + .append('<small class="form-text text-muted">Hint: At what time you were born?</small>') |
| 107 | + ) |
| 108 | + ) |
| 109 | + |
| 110 | + $('form#new_user').validate() |
| 111 | + } |
| 112 | +}) |
| 113 | + |
| 114 | +//I don think this multiple wrapper names makes sense here, because in SimpleForm different wrapper would have different DOM |
| 115 | +//but here DOM ($('#qunit-fixture')) is same for all tests. |
| 116 | +var wrappers = ['vertical_form'] |
| 117 | + |
| 118 | +for (var i = 0; i < wrappers.length; i++) { |
| 119 | + var wrapper = wrappers[i] |
| 120 | + |
| 121 | + QUnit.test(wrapper + ' - Validate date input', function (assert) { |
| 122 | + const form = $('form#new_user'); |
| 123 | + const select_year = form.find('select#user_date_of_birth_1i') |
| 124 | + const select_month = form.find('select#user_date_of_birth_2i') |
| 125 | + const select_day = form.find('select#user_date_of_birth_3i') |
| 126 | + |
| 127 | + form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper |
| 128 | + |
| 129 | + select_year.trigger('focusout') |
| 130 | + select_day.trigger('focusout') |
| 131 | + |
| 132 | + assert.ok(select_year.closest('.form-group').hasClass('form-group-invalid')) |
| 133 | + assert.ok(select_month.closest('.form-group').hasClass('form-group-invalid')) |
| 134 | + assert.ok(select_year.closest('.form-group').find('div.invalid-feedback').length === 1) |
| 135 | + |
| 136 | + select_year.val(2025).trigger('change').trigger('focusout') |
| 137 | + |
| 138 | + assert.ok(select_year.closest('.form-group').hasClass('form-group-invalid')) |
| 139 | + assert.ok(select_year.closest('.form-group').find('div.invalid-feedback').length === 1) |
| 140 | + |
| 141 | + select_month.val(1).trigger('change').trigger('focusout') |
| 142 | + select_day.val(1).trigger('change').trigger('focusout') |
| 143 | + |
| 144 | + assert.notOk(select_year.closest('.form-group').hasClass('form-group-invalid')) |
| 145 | + assert.ok(select_year.closest('.form-group').find('div.invalid-feedback').length === 0) |
| 146 | + }) |
| 147 | + |
| 148 | + QUnit.test(wrapper + ' - Validate time input reusing server message', function (assert) { |
| 149 | + const form = $('form#new_user'); |
| 150 | + const select_hour = form.find('select#user_time_of_birth_4i') |
| 151 | + const select_minute = form.find('select#user_time_of_birth_5i') |
| 152 | + |
| 153 | + form[0].ClientSideValidations.settings.html_settings.wrapper = wrapper |
| 154 | + |
| 155 | + assert.ok(select_hour.closest('.form-group').hasClass('form-group-invalid')) |
| 156 | + assert.ok(select_minute.closest('.form-group').hasClass('form-group-invalid')) |
| 157 | + assert.ok(select_hour.closest('.form-group').find('div.invalid-feedback').length === 1) |
| 158 | + |
| 159 | + select_hour.val(23).trigger('change').trigger('focusout') |
| 160 | + |
| 161 | + assert.ok(select_hour.closest('.form-group').hasClass('form-group-invalid')) |
| 162 | + assert.ok(select_minute.closest('.form-group').hasClass('form-group-invalid')) |
| 163 | + assert.ok(select_hour.closest('.form-group').find('div.invalid-feedback').length === 1) |
| 164 | + |
| 165 | + select_minute.val(55).trigger('change').trigger('focusout') |
| 166 | + |
| 167 | + assert.notOk(select_hour.closest('.form-group').hasClass('form-group-invalid')) |
| 168 | + assert.ok(select_hour.closest('.form-group').find('div.invalid-feedback').length === 0) |
| 169 | + }) |
| 170 | +} |
0 commit comments