Skip to content

Commit

Permalink
Support javascript case-sensitive uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed Feb 28, 2019
1 parent d219af2 commit 645b8b0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
8 changes: 7 additions & 1 deletion coffeescript/rails.validations.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 41 additions & 2 deletions test/javascript/public/test/validators/uniqueness.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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: '<div class="field_with_errors"><span id="input_tag" /><label for="user_name" class="message"></label></div>',
label_tag: '<div class="field_with_errors"><label id="label_tag" /></div>'
},
validators: { 'user[email]':{"uniqueness":[{"message": "must be unique"}]}}
}

$('#qunit-fixture')
.append($('<form />', {
action: '/users',
'data-client-side-validations': JSON.stringify(dataCsv),
method: 'post',
id: 'new_user_3'
}))
.find('form')
.append($('<input />', {
name: 'profile[user_attributes][0][email]',
id: 'user_0_email',
}))
.append($('<input />', {
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);
});
8 changes: 7 additions & 1 deletion vendor/assets/javascripts/rails.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 645b8b0

Please sign in to comment.