* Sets the value. Filters and validates value and updates the valid flag. * @param {mixed} rawValue Field value * @param {?object} [sender] Sender object. Only delegates that are not equal * to the sender will be notified. * @return {Field} Fluent interface
(rawValue, sender = null)
| 197 | * @return {Field} Fluent interface |
| 198 | */ |
| 199 | setValue (rawValue, sender = null) { |
| 200 | // Validate value |
| 201 | const validationResult = this.validateValue(rawValue) |
| 202 | this._valid = validationResult === true |
| 203 | |
| 204 | // Update message |
| 205 | if (!this._valid) { |
| 206 | if (typeof validationResult === 'object') { |
| 207 | this._message = validationResult.message |
| 208 | this._messageKey = validationResult.key |
| 209 | } else { |
| 210 | this._message = 'The value is invalid' |
| 211 | this._messageKey = 'invalid' |
| 212 | } |
| 213 | } else { |
| 214 | this._message = null |
| 215 | this._messageKey = null |
| 216 | } |
| 217 | |
| 218 | // Update message on view |
| 219 | this.hasView() && this.getView().setMessage(this.getMessage()) |
| 220 | |
| 221 | if (!this._valid) { |
| 222 | this._value = rawValue |
| 223 | return this |
| 224 | } |
| 225 | |
| 226 | // Filter value |
| 227 | const value = this.filterValue(rawValue) |
| 228 | |
| 229 | // Check if value has changed |
| 230 | // Use the special comparison method when dealing with chain instances |
| 231 | const equal = value instanceof Chain |
| 232 | ? value.isEqualTo(this._value) |
| 233 | : this._value === value |
| 234 | |
| 235 | if (!equal) { |
| 236 | this._value = value |
| 237 | |
| 238 | // Update the value in view |
| 239 | if (this.hasView() && this.getView() !== sender) { |
| 240 | this.getView().updateValue() |
| 241 | } |
| 242 | |
| 243 | // Notify delegate, if it is interested |
| 244 | if (this._delegate && |
| 245 | this._delegate !== sender && |
| 246 | this._delegate.fieldValueDidChange !== undefined) { |
| 247 | this.getDelegate().fieldValueDidChange(this, value) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return this |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Revalidates the current value, sets {@link Field.isValid} flag to |
no test coverage detected