Skip to content

Commit 442b8b0

Browse files
Issue #504 fixed
1 parent f27d8c8 commit 442b8b0

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

packages/Webkul/Admin/src/Resources/lang/en/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@
800800
'select-call' => 'Call',
801801
'select-meeting' => 'Meeting',
802802
'select-lunch' => 'Lunch',
803+
'duplicate-value' => 'The value cannot be duplicate'
803804
],
804805

805806
'user' => [

packages/Webkul/Admin/src/Resources/views/common/custom-attributes/edit/email.blade.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class="form-group input-group"
2121
:name="attribute['code'] + '[' + index + '][value]'"
2222
class="control"
2323
v-model="email['value']"
24-
v-validate="validations"
24+
v-validate="validations + '|unique_email'"
2525
:data-vv-as="attribute['name']"
2626
>
2727

@@ -67,6 +67,8 @@ class="control"
6767
},
6868
6969
created: function() {
70+
this.extendValidator();
71+
7072
if (! this.emails || ! this.emails.length) {
7173
this.emails = [{
7274
'value': '',
@@ -87,6 +89,44 @@ class="control"
8789
const index = this.emails.indexOf(email);
8890
8991
Vue.delete(this.emails, index);
92+
},
93+
94+
extendValidator: function () {
95+
this.$validator.extend('unique_email', {
96+
getMessage: (field) => '{!! __('admin::app.common.duplicate-value') !!}',
97+
98+
validate: (value) => {
99+
let filteredEmails = this.emails.filter((email) => {
100+
return email.value == value;
101+
});
102+
103+
if (filteredEmails.length > 1) {
104+
return false;
105+
}
106+
107+
this.removeUniqueErrors();
108+
109+
return true;
110+
}
111+
});
112+
},
113+
114+
isDuplicateExists: function () {
115+
let emails = this.emails.map((email) => email.value);
116+
117+
return emails.some((email, index) => emails.indexOf(email) != index);
118+
},
119+
120+
removeUniqueErrors: function () {
121+
if (! this.isDuplicateExists()) {
122+
this.errors
123+
.items
124+
.filter(error => error.rule === 'unique')
125+
.map(error => error.id)
126+
.forEach((id) => {
127+
this.errors.removeById(id);
128+
});
129+
}
90130
}
91131
}
92132
});

packages/Webkul/Admin/src/Resources/views/common/custom-attributes/edit/phone.blade.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class="form-group input-group"
2222
:name="attribute['code'] + '[' + index + '][value]'"
2323
class="control"
2424
v-model="contactNumber['value']"
25-
v-validate="validations"
25+
v-validate="validations + '|unique_contact_number'"
2626
:data-vv-as="attribute['name']"
2727
/>
2828

@@ -72,6 +72,8 @@ class="control"
7272
},
7373
7474
created: function() {
75+
this.extendValidator();
76+
7577
if (! this.contactNumbers || ! this.contactNumbers.length) {
7678
this.contactNumbers = [{
7779
'value': '',
@@ -92,6 +94,44 @@ class="control"
9294
const index = this.contactNumbers.indexOf(contactNumber);
9395
9496
Vue.delete(this.contactNumbers, index);
97+
},
98+
99+
extendValidator: function () {
100+
this.$validator.extend('unique_contact_number', {
101+
getMessage: (field) => '{!! __('admin::app.common.duplicate-value') !!}',
102+
103+
validate: (value) => {
104+
let filteredContactNumbers = this.contactNumbers.filter((contactNumber) => {
105+
return contactNumber.value == value;
106+
});
107+
108+
if (filteredContactNumbers.length > 1) {
109+
return false;
110+
}
111+
112+
this.removeUniqueErrors();
113+
114+
return true;
115+
}
116+
});
117+
},
118+
119+
isDuplicateExists: function () {
120+
let contactNumbers = this.contactNumbers.map((contactNumber) => contactNumber.value);
121+
122+
return contactNumbers.some((contactNumber, index) => contactNumbers.indexOf(contactNumber) != index);
123+
},
124+
125+
removeUniqueErrors: function () {
126+
if (! this.isDuplicateExists()) {
127+
this.errors
128+
.items
129+
.filter(error => error.rule === 'unique')
130+
.map(error => error.id)
131+
.forEach((id) => {
132+
this.errors.removeById(id);
133+
});
134+
}
95135
}
96136
}
97137
});

0 commit comments

Comments
 (0)