From 645b8b090e4d78ca7007a6bf4249a01bffd6b125 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Thu, 28 Feb 2019 21:24:55 +0100 Subject: [PATCH] Support javascript case-sensitive uniqueness --- coffeescript/rails.validations.coffee | 8 +++- .../public/test/validators/uniqueness.js | 43 ++++++++++++++++++- .../assets/javascripts/rails.validations.js | 8 +++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/coffeescript/rails.validations.coffee b/coffeescript/rails.validations.coffee index 644a33d36..a5d495297 100644 --- a/coffeescript/rails.validations.coffee +++ b/coffeescript/rails.validations.coffee @@ -413,8 +413,14 @@ ClientSideValidations = valid = true form.find(":input[name^=\"#{name_prefix}\"][name$=\"#{name_suffix}\"]").each -> + other_value = $(@).val() + + unless options.case_sensitive + value = value.toLowerCase() + other_value = other_value.toLowerCase() + if $(@).attr('name') != name - if $(@).val() == value + if other_value == value valid = false $(@).data('notLocallyUnique', true) else diff --git a/test/javascript/public/test/validators/uniqueness.js b/test/javascript/public/test/validators/uniqueness.js index 20ecd1d45..7a93c70bc 100644 --- a/test/javascript/public/test/validators/uniqueness.js +++ b/test/javascript/public/test/validators/uniqueness.js @@ -33,7 +33,7 @@ QUnit.module('Uniqueness options', { } }); -QUnit.test('when matching local uniqueness for nested has-many resources', function(assert) { +QUnit.test('when matching local case-insensitive uniqueness for nested has-many resources', function(assert) { dataCsv = { html_settings: { type: 'ActionView::Helpers::FormBuilder', @@ -67,7 +67,46 @@ QUnit.test('when matching local uniqueness for nested has-many resources', funct options = { 'message': "must be unique" }; user_0_email.val('not-locally-unique'); - user_1_email.val('not-locally-unique'); + user_1_email.val('Not-Locally-Unique'); assert.equal(ClientSideValidations.validators.local.uniqueness(user_1_email, options), "must be unique"); }); + +QUnit.test('when matching case-sensitive local uniqueness for nested has-many resources', function(assert) { + dataCsv = { + html_settings: { + type: 'ActionView::Helpers::FormBuilder', + input_tag: '
', + label_tag: '
' + }, + validators: { 'user[email]':{"uniqueness":[{"message": "must be unique"}]}} + } + + $('#qunit-fixture') + .append($('
', { + action: '/users', + 'data-client-side-validations': JSON.stringify(dataCsv), + method: 'post', + id: 'new_user_3' + })) + .find('form') + .append($('', { + name: 'profile[user_attributes][0][email]', + id: 'user_0_email', + })) + .append($('', { + name: 'profile[user_attributes][1][email]', + id: 'user_1_email', + })); + + $('form#new_user_3').validate(); + + var user_0_email = $('#user_0_email'), + user_1_email = $('#user_1_email'), + options = { 'message': "must be unique", "case_sensitive": true }; + + user_0_email.val('locally-unique'); + user_1_email.val('Locally-Unique'); + + assert.equal(ClientSideValidations.validators.local.uniqueness(user_1_email, options), undefined); +}); diff --git a/vendor/assets/javascripts/rails.validations.js b/vendor/assets/javascripts/rails.validations.js index 37351e3e4..b84a5c643 100644 --- a/vendor/assets/javascripts/rails.validations.js +++ b/vendor/assets/javascripts/rails.validations.js @@ -535,8 +535,14 @@ form = element.closest('form'); valid = true; form.find(":input[name^=\"" + name_prefix + "\"][name$=\"" + name_suffix + "\"]").each(function() { + var other_value; + other_value = $(this).val(); + if (!options.case_sensitive) { + value = value.toLowerCase(); + other_value = other_value.toLowerCase(); + } if ($(this).attr('name') !== name) { - if ($(this).val() === value) { + if (other_value === value) { valid = false; return $(this).data('notLocallyUnique', true); } else {