|
| 1 | +import Ember from 'ember'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Simple mixin that wrap the logic of getting and rendering an input error message |
| 5 | + */ |
| 6 | +export default Ember.Mixin.create({ |
| 7 | + /** |
| 8 | + * Title attribute is needed for providing a custom message |
| 9 | + * |
| 10 | + * @type {Array} |
| 11 | + */ |
| 12 | + attributeBindings: ['title'], |
| 13 | + |
| 14 | + /** |
| 15 | + * Decide if we show the native browser error messages |
| 16 | + * |
| 17 | + * @type {boolean} |
| 18 | + */ |
| 19 | + useBrowserMessages: false, |
| 20 | + |
| 21 | + /** |
| 22 | + * Current error message for the field |
| 23 | + * |
| 24 | + * @type {string} |
| 25 | + */ |
| 26 | + errorMessage: null, |
| 27 | + |
| 28 | + /** |
| 29 | + * Allow to override error messages |
| 30 | + * |
| 31 | + * @type {Object} |
| 32 | + */ |
| 33 | + errorTemplates: { |
| 34 | + // Errors when an input with "required" attribute has no value |
| 35 | + valueMissing: { |
| 36 | + defaultMessage: 'Value is required', |
| 37 | + checkbox: 'You must check this box', |
| 38 | + select: 'You must select at least an option', |
| 39 | + radio: 'You must select an option' |
| 40 | + }, |
| 41 | + |
| 42 | + // Errors when a value does not match a given type like "url" or "email" |
| 43 | + typeMismatch: { |
| 44 | + defaultMessage: 'Value is invalid', |
| 45 | + email: 'Email address is invalid', |
| 46 | + url: 'URL is invalid' |
| 47 | + }, |
| 48 | + |
| 49 | + // Errors when a value does not follow the "pattern" regex |
| 50 | + patternMismatch: { |
| 51 | + defaultMessage: 'Value does not follow expected pattern' |
| 52 | + }, |
| 53 | + |
| 54 | + // Errors when an input is too long |
| 55 | + tooLong: { |
| 56 | + defaultMessage: 'Enter at most %@ characters' |
| 57 | + }, |
| 58 | + |
| 59 | + // Errors when an input is less than "min" value |
| 60 | + rangeUnderflow: { |
| 61 | + defaultMessage: 'Number must be more than %@' |
| 62 | + }, |
| 63 | + |
| 64 | + // Errors when an input is more than "max" value |
| 65 | + rangeOverflow: { |
| 66 | + defaultMessage: 'Number must be less than %@' |
| 67 | + }, |
| 68 | + |
| 69 | + // Errors when a value does not follow step (for instance for "range" type) |
| 70 | + stepMismatch: { |
| 71 | + defaultMessage: 'Value is invalid' |
| 72 | + }, |
| 73 | + |
| 74 | + // Default message that is used when none is matched |
| 75 | + defaultMessage: 'Value is invalid' |
| 76 | + }, |
| 77 | + |
| 78 | + /** |
| 79 | + * @returns {void} |
| 80 | + */ |
| 81 | + attachValidationListener: function() { |
| 82 | + Ember.$(this.get('element')).on('invalid', Ember.run.bind(this, this.validate)); |
| 83 | + }.on('didInsertElement'), |
| 84 | + |
| 85 | + /** |
| 86 | + * @returns {void} |
| 87 | + */ |
| 88 | + detachValidationListener: function() { |
| 89 | + Ember.$(this.get('element')).off('invalid'); |
| 90 | + }.on('willDestroyElement'), |
| 91 | + |
| 92 | + /** |
| 93 | + * Validate the input whenever it looses focus |
| 94 | + * |
| 95 | + * @returns {void} |
| 96 | + */ |
| 97 | + validate: function() { |
| 98 | + var input = this.get('element'); |
| 99 | + |
| 100 | + // According to spec, inputs that have "formnovalidate" should bypass any validation |
| 101 | + if (input.hasAttribute('formnovalidate')) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if (!input.validity.valid && !input.validity.customError) { |
| 106 | + this.set('errorMessage', this.getErrorMessage()); |
| 107 | + } else { |
| 108 | + this.set('errorMessage', null); |
| 109 | + input.setCustomValidity(''); |
| 110 | + } |
| 111 | + }.on('focusOut'), |
| 112 | + |
| 113 | + /** |
| 114 | + * Set a custom error message for the input. Note that we set the error message directly, as well as we |
| 115 | + * set the error using setCustomValidity, so that a call to checkValidate evaluate to false |
| 116 | + * |
| 117 | + * @type {string} error |
| 118 | + * @returns {void} |
| 119 | + */ |
| 120 | + setCustomErrorMessage: function(error) { |
| 121 | + this.set('errorMessage', error); |
| 122 | + this.get('element').setCustomValidity(error); |
| 123 | + }, |
| 124 | + |
| 125 | + /** |
| 126 | + * Render the error message bound to the field (or remove if it is null) |
| 127 | + * |
| 128 | + * @TODO: this should be done in a more flexible way to allow custom template |
| 129 | + */ |
| 130 | + renderErrorMessage: function() { |
| 131 | + var element = this.$(), |
| 132 | + errorMessage = this.get('errorMessage'); |
| 133 | + |
| 134 | + if (null === errorMessage) { |
| 135 | + element.removeClass('invalid'); |
| 136 | + element.siblings('.input-error').remove(); |
| 137 | + } else { |
| 138 | + element.siblings('.input-error').remove(); |
| 139 | + element.addClass('invalid'); |
| 140 | + element.after('<p class="input-error">' + errorMessage + '</p>'); |
| 141 | + } |
| 142 | + }.observes('errorMessage'), |
| 143 | + |
| 144 | + /** |
| 145 | + * Get the message error |
| 146 | + * |
| 147 | + * @returns {String} |
| 148 | + */ |
| 149 | + getErrorMessage: function() { |
| 150 | + var target = this.get('element'); |
| 151 | + |
| 152 | + // If user want to use native browser error messages, we directly return |
| 153 | + if (this.get('useBrowserMessages')) { |
| 154 | + return target.validationMessage; |
| 155 | + } |
| 156 | + |
| 157 | + var errorTemplates = this.get('errorTemplates'), |
| 158 | + type = target.getAttribute('type'); |
| 159 | + |
| 160 | + // We first check for the "required" case |
| 161 | + if (target.validity.valueMissing) { |
| 162 | + // For checkbox, we allow to have a title attribute that is shown instead of the |
| 163 | + // required message. Very useful for things like "You must accept our terms" |
| 164 | + if (type === 'checkbox' && target.hasAttribute('title')) { |
| 165 | + return target.getAttribute('title'); |
| 166 | + } |
| 167 | + |
| 168 | + return errorTemplates.valueMissing[type] || errorTemplates.valueMissing['defaultMessage']; |
| 169 | + } |
| 170 | + |
| 171 | + // If a "title" attribute has been set, according to the spec, we can use it as the message |
| 172 | + if (target.hasAttribute('title')) { |
| 173 | + return target.getAttribute('title'); |
| 174 | + } |
| 175 | + |
| 176 | + var errorKeys = ['stepMismatch', 'rangeOverflow', 'rangeUnderflow', 'tooLong', 'patternMismatch', 'typeMismatch']; |
| 177 | + |
| 178 | + for (var i = 0 ; i !== errorKeys.length ; ++i) { |
| 179 | + var errorKey = errorKeys[i]; |
| 180 | + |
| 181 | + if (!target.validity[errorKey]) { |
| 182 | + continue; |
| 183 | + } |
| 184 | + |
| 185 | + var message = errorTemplates[errorKey][type] || errorTemplates[errorKey]['defaultMessage']; |
| 186 | + |
| 187 | + switch (errorKey) { |
| 188 | + case 'tooLong': |
| 189 | + return message.fmt(target.getAttribute('maxlength')); |
| 190 | + case 'rangeUnderflow': |
| 191 | + return message.fmt(target.getAttribute('min')); |
| 192 | + case 'rangeOverflow': |
| 193 | + return message.fmt(target.getAttribute('max')); |
| 194 | + default: |
| 195 | + return message; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return errorTemplates.defaultMessage; |
| 200 | + } |
| 201 | +}); |
0 commit comments