Field.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-form-field-Field'>/**
  19. </span> * @docauthor Jason Johnston &lt;jason@sencha.com&gt;
  20. *
  21. * This mixin provides a common interface for the logical behavior and state of form fields, including:
  22. *
  23. * - Getter and setter methods for field values
  24. * - Events and methods for tracking value and validity changes
  25. * - Methods for triggering validation
  26. *
  27. * **NOTE**: When implementing custom fields, it is most likely that you will want to extend the {@link Ext.form.field.Base}
  28. * component class rather than using this mixin directly, as BaseField contains additional logic for generating an
  29. * actual DOM complete with {@link Ext.form.Labelable label and error message} display and a form input field,
  30. * plus methods that bind the Field value getters and setters to the input field's value.
  31. *
  32. * If you do want to implement this mixin directly and don't want to extend {@link Ext.form.field.Base}, then
  33. * you will most likely want to override the following methods with custom implementations: {@link #getValue},
  34. * {@link #setValue}, and {@link #getErrors}. Other methods may be overridden as needed but their base
  35. * implementations should be sufficient for common cases. You will also need to make sure that {@link #initField}
  36. * is called during the component's initialization.
  37. */
  38. Ext.define('Ext.form.field.Field', {
  39. <span id='Ext-form-field-Field-property-isFormField'> /**
  40. </span> * @property {Boolean} isFormField
  41. * Flag denoting that this component is a Field. Always true.
  42. */
  43. isFormField : true,
  44. <span id='Ext-form-field-Field-cfg-value'> /**
  45. </span> * @cfg {Object} value
  46. * A value to initialize this field with.
  47. */
  48. <span id='Ext-form-field-Field-cfg-name'> /**
  49. </span> * @cfg {String} name
  50. * The name of the field. By default this is used as the parameter name when including the
  51. * {@link #getSubmitData field value} in a {@link Ext.form.Basic#submit form submit()}. To prevent the field from
  52. * being included in the form submit, set {@link #submitValue} to false.
  53. */
  54. <span id='Ext-form-field-Field-cfg-disabled'> /**
  55. </span> * @cfg {Boolean} disabled
  56. * True to disable the field. Disabled Fields will not be {@link Ext.form.Basic#submit submitted}.
  57. */
  58. disabled : false,
  59. <span id='Ext-form-field-Field-cfg-submitValue'> /**
  60. </span> * @cfg {Boolean} submitValue
  61. * Setting this to false will prevent the field from being {@link Ext.form.Basic#submit submitted} even when it is
  62. * not disabled.
  63. */
  64. submitValue: true,
  65. <span id='Ext-form-field-Field-cfg-validateOnChange'> /**
  66. </span> * @cfg {Boolean} validateOnChange
  67. * Specifies whether this field should be validated immediately whenever a change in its value is detected.
  68. * If the validation results in a change in the field's validity, a {@link #validitychange} event will be
  69. * fired. This allows the field to show feedback about the validity of its contents immediately as the user is
  70. * typing.
  71. *
  72. * When set to false, feedback will not be immediate. However the form will still be validated before submitting if
  73. * the clientValidation option to {@link Ext.form.Basic#doAction} is enabled, or if the field or form are validated
  74. * manually.
  75. *
  76. * See also {@link Ext.form.field.Base#checkChangeEvents} for controlling how changes to the field's value are
  77. * detected.
  78. */
  79. validateOnChange: true,
  80. <span id='Ext-form-field-Field-property-suspendCheckChange'> /**
  81. </span> * @private
  82. */
  83. suspendCheckChange: 0,
  84. <span id='Ext-form-field-Field-method-initField'> /**
  85. </span> * Initializes this Field mixin on the current instance. Components using this mixin should call this method during
  86. * their own initialization process.
  87. */
  88. initField: function() {
  89. this.addEvents(
  90. <span id='Ext-form-field-Field-event-change'> /**
  91. </span> * @event change
  92. * Fires when the value of a field is changed via the {@link #setValue} method.
  93. * @param {Ext.form.field.Field} this
  94. * @param {Object} newValue The new value
  95. * @param {Object} oldValue The original value
  96. */
  97. 'change',
  98. <span id='Ext-form-field-Field-event-validitychange'> /**
  99. </span> * @event validitychange
  100. * Fires when a change in the field's validity is detected.
  101. * @param {Ext.form.field.Field} this
  102. * @param {Boolean} isValid Whether or not the field is now valid
  103. */
  104. 'validitychange',
  105. <span id='Ext-form-field-Field-event-dirtychange'> /**
  106. </span> * @event dirtychange
  107. * Fires when a change in the field's {@link #isDirty} state is detected.
  108. * @param {Ext.form.field.Field} this
  109. * @param {Boolean} isDirty Whether or not the field is now dirty
  110. */
  111. 'dirtychange'
  112. );
  113. this.initValue();
  114. },
  115. <span id='Ext-form-field-Field-method-initValue'> /**
  116. </span> * Initializes the field's value based on the initial config.
  117. */
  118. initValue: function() {
  119. var me = this;
  120. me.value = me.transformOriginalValue(me.value);
  121. <span id='Ext-form-field-Field-property-originalValue'> /**
  122. </span> * @property {Object} originalValue
  123. * The original value of the field as configured in the {@link #value} configuration, or as loaded by the last
  124. * form load operation if the form's {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad} setting is `true`.
  125. */
  126. me.originalValue = me.lastValue = me.value;
  127. // Set the initial value - prevent validation on initial set
  128. me.suspendCheckChange++;
  129. me.setValue(me.value);
  130. me.suspendCheckChange--;
  131. },
  132. <span id='Ext-form-field-Field-method-transformOriginalValue'> /**
  133. </span> * Allows for any necessary modifications before the original
  134. * value is set
  135. * @protected
  136. * @param {Object} value The initial value
  137. * @return {Object} The modified initial value
  138. */
  139. transformOriginalValue: function(value){
  140. return value;
  141. },
  142. <span id='Ext-form-field-Field-method-getName'> /**
  143. </span> * Returns the {@link Ext.form.field.Field#name name} attribute of the field. This is used as the parameter name
  144. * when including the field value in a {@link Ext.form.Basic#submit form submit()}.
  145. * @return {String} name The field {@link Ext.form.field.Field#name name}
  146. */
  147. getName: function() {
  148. return this.name;
  149. },
  150. <span id='Ext-form-field-Field-method-getValue'> /**
  151. </span> * Returns the current data value of the field. The type of value returned is particular to the type of the
  152. * particular field (e.g. a Date object for {@link Ext.form.field.Date}).
  153. * @return {Object} value The field value
  154. */
  155. getValue: function() {
  156. return this.value;
  157. },
  158. <span id='Ext-form-field-Field-method-setValue'> /**
  159. </span> * Sets a data value into the field and runs the change detection and validation.
  160. * @param {Object} value The value to set
  161. * @return {Ext.form.field.Field} this
  162. */
  163. setValue: function(value) {
  164. var me = this;
  165. me.value = value;
  166. me.checkChange();
  167. return me;
  168. },
  169. <span id='Ext-form-field-Field-method-isEqual'> /**
  170. </span> * Returns whether two field {@link #getValue values} are logically equal. Field implementations may override this
  171. * to provide custom comparison logic appropriate for the particular field's data type.
  172. * @param {Object} value1 The first value to compare
  173. * @param {Object} value2 The second value to compare
  174. * @return {Boolean} True if the values are equal, false if inequal.
  175. */
  176. isEqual: function(value1, value2) {
  177. return String(value1) === String(value2);
  178. },
  179. <span id='Ext-form-field-Field-method-isEqualAsString'> /**
  180. </span> * Returns whether two values are logically equal.
  181. * Similar to {@link #isEqual}, however null or undefined values will be treated as empty strings.
  182. * @private
  183. * @param {Object} value1 The first value to compare
  184. * @param {Object} value2 The second value to compare
  185. * @return {Boolean} True if the values are equal, false if inequal.
  186. */
  187. isEqualAsString: function(value1, value2){
  188. return String(Ext.value(value1, '')) === String(Ext.value(value2, ''));
  189. },
  190. <span id='Ext-form-field-Field-method-getSubmitData'> /**
  191. </span> * Returns the parameter(s) that would be included in a standard form submit for this field. Typically this will be
  192. * an object with a single name-value pair, the name being this field's {@link #getName name} and the value being
  193. * its current stringified value. More advanced field implementations may return more than one name-value pair.
  194. *
  195. * Note that the values returned from this method are not guaranteed to have been successfully {@link #validate
  196. * validated}.
  197. *
  198. * @return {Object} A mapping of submit parameter names to values; each value should be a string, or an array of
  199. * strings if that particular name has multiple values. It can also return null if there are no parameters to be
  200. * submitted.
  201. */
  202. getSubmitData: function() {
  203. var me = this,
  204. data = null;
  205. if (!me.disabled &amp;&amp; me.submitValue &amp;&amp; !me.isFileUpload()) {
  206. data = {};
  207. data[me.getName()] = '' + me.getValue();
  208. }
  209. return data;
  210. },
  211. <span id='Ext-form-field-Field-method-getModelData'> /**
  212. </span> * Returns the value(s) that should be saved to the {@link Ext.data.Model} instance for this field, when {@link
  213. * Ext.form.Basic#updateRecord} is called. Typically this will be an object with a single name-value pair, the name
  214. * being this field's {@link #getName name} and the value being its current data value. More advanced field
  215. * implementations may return more than one name-value pair. The returned values will be saved to the corresponding
  216. * field names in the Model.
  217. *
  218. * Note that the values returned from this method are not guaranteed to have been successfully {@link #validate
  219. * validated}.
  220. *
  221. * @return {Object} A mapping of submit parameter names to values; each value should be a string, or an array of
  222. * strings if that particular name has multiple values. It can also return null if there are no parameters to be
  223. * submitted.
  224. */
  225. getModelData: function() {
  226. var me = this,
  227. data = null;
  228. if (!me.disabled &amp;&amp; !me.isFileUpload()) {
  229. data = {};
  230. data[me.getName()] = me.getValue();
  231. }
  232. return data;
  233. },
  234. <span id='Ext-form-field-Field-method-reset'> /**
  235. </span> * Resets the current field value to the originally loaded value and clears any validation messages. See {@link
  236. * Ext.form.Basic}.{@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad}
  237. */
  238. reset : function(){
  239. var me = this;
  240. me.beforeReset();
  241. me.setValue(me.originalValue);
  242. me.clearInvalid();
  243. // delete here so we reset back to the original state
  244. delete me.wasValid;
  245. },
  246. <span id='Ext-form-field-Field-method-beforeReset'> /**
  247. </span> * Template method before a field is reset.
  248. * @protected
  249. */
  250. beforeReset: Ext.emptyFn,
  251. <span id='Ext-form-field-Field-method-resetOriginalValue'> /**
  252. </span> * Resets the field's {@link #originalValue} property so it matches the current {@link #getValue value}. This is
  253. * called by {@link Ext.form.Basic}.{@link Ext.form.Basic#setValues setValues} if the form's
  254. * {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad} property is set to true.
  255. */
  256. resetOriginalValue: function() {
  257. this.originalValue = this.getValue();
  258. this.checkDirty();
  259. },
  260. <span id='Ext-form-field-Field-method-checkChange'> /**
  261. </span> * Checks whether the value of the field has changed since the last time it was checked.
  262. * If the value has changed, it:
  263. *
  264. * 1. Fires the {@link #change change event},
  265. * 2. Performs validation if the {@link #validateOnChange} config is enabled, firing the
  266. * {@link #validitychange validitychange event} if the validity has changed, and
  267. * 3. Checks the {@link #isDirty dirty state} of the field and fires the {@link #dirtychange dirtychange event}
  268. * if it has changed.
  269. */
  270. checkChange: function() {
  271. if (!this.suspendCheckChange) {
  272. var me = this,
  273. newVal = me.getValue(),
  274. oldVal = me.lastValue;
  275. if (!me.isEqual(newVal, oldVal) &amp;&amp; !me.isDestroyed) {
  276. me.lastValue = newVal;
  277. me.fireEvent('change', me, newVal, oldVal);
  278. me.onChange(newVal, oldVal);
  279. }
  280. }
  281. },
  282. <span id='Ext-form-field-Field-method-onChange'> /**
  283. </span> * @private
  284. * Called when the field's value changes. Performs validation if the {@link #validateOnChange}
  285. * config is enabled, and invokes the dirty check.
  286. */
  287. onChange: function(newVal, oldVal) {
  288. if (this.validateOnChange) {
  289. this.validate();
  290. }
  291. this.checkDirty();
  292. },
  293. <span id='Ext-form-field-Field-method-isDirty'> /**
  294. </span> * Returns true if the value of this Field has been changed from its {@link #originalValue}.
  295. * Will always return false if the field is disabled.
  296. *
  297. * Note that if the owning {@link Ext.form.Basic form} was configured with
  298. * {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad} then the {@link #originalValue} is updated when
  299. * the values are loaded by {@link Ext.form.Basic}.{@link Ext.form.Basic#setValues setValues}.
  300. * @return {Boolean} True if this field has been changed from its original value (and is not disabled),
  301. * false otherwise.
  302. */
  303. isDirty : function() {
  304. var me = this;
  305. return !me.disabled &amp;&amp; !me.isEqual(me.getValue(), me.originalValue);
  306. },
  307. <span id='Ext-form-field-Field-method-checkDirty'> /**
  308. </span> * Checks the {@link #isDirty} state of the field and if it has changed since the last time it was checked,
  309. * fires the {@link #dirtychange} event.
  310. */
  311. checkDirty: function() {
  312. var me = this,
  313. isDirty = me.isDirty();
  314. if (isDirty !== me.wasDirty) {
  315. me.fireEvent('dirtychange', me, isDirty);
  316. me.onDirtyChange(isDirty);
  317. me.wasDirty = isDirty;
  318. }
  319. },
  320. <span id='Ext-form-field-Field-method-onDirtyChange'> /**
  321. </span> * @private Called when the field's dirty state changes.
  322. * @param {Boolean} isDirty
  323. */
  324. onDirtyChange: Ext.emptyFn,
  325. <span id='Ext-form-field-Field-method-getErrors'> /**
  326. </span> * Runs this field's validators and returns an array of error messages for any validation failures. This is called
  327. * internally during validation and would not usually need to be used manually.
  328. *
  329. * Each subclass should override or augment the return value to provide their own errors.
  330. *
  331. * @param {Object} value The value to get errors for (defaults to the current field value)
  332. * @return {String[]} All error messages for this field; an empty Array if none.
  333. */
  334. getErrors: function(value) {
  335. return [];
  336. },
  337. <span id='Ext-form-field-Field-method-isValid'> /**
  338. </span> * Returns whether or not the field value is currently valid by {@link #getErrors validating} the field's current
  339. * value. The {@link #validitychange} event will not be fired; use {@link #validate} instead if you want the event
  340. * to fire. **Note**: {@link #disabled} fields are always treated as valid.
  341. *
  342. * Implementations are encouraged to ensure that this method does not have side-effects such as triggering error
  343. * message display.
  344. *
  345. * @return {Boolean} True if the value is valid, else false
  346. */
  347. isValid : function() {
  348. var me = this;
  349. return me.disabled || Ext.isEmpty(me.getErrors());
  350. },
  351. <span id='Ext-form-field-Field-method-validate'> /**
  352. </span> * Returns whether or not the field value is currently valid by {@link #getErrors validating} the field's current
  353. * value, and fires the {@link #validitychange} event if the field's validity has changed since the last validation.
  354. * **Note**: {@link #disabled} fields are always treated as valid.
  355. *
  356. * Custom implementations of this method are allowed to have side-effects such as triggering error message display.
  357. * To validate without side-effects, use {@link #isValid}.
  358. *
  359. * @return {Boolean} True if the value is valid, else false
  360. */
  361. validate : function() {
  362. var me = this,
  363. isValid = me.isValid();
  364. if (isValid !== me.wasValid) {
  365. me.wasValid = isValid;
  366. me.fireEvent('validitychange', me, isValid);
  367. }
  368. return isValid;
  369. },
  370. <span id='Ext-form-field-Field-method-batchChanges'> /**
  371. </span> * A utility for grouping a set of modifications which may trigger value changes into a single transaction, to
  372. * prevent excessive firing of {@link #change} events. This is useful for instance if the field has sub-fields which
  373. * are being updated as a group; you don't want the container field to check its own changed state for each subfield
  374. * change.
  375. * @param {Object} fn A function containing the transaction code
  376. */
  377. batchChanges: function(fn) {
  378. try {
  379. this.suspendCheckChange++;
  380. fn();
  381. } catch(e){
  382. throw e;
  383. } finally {
  384. this.suspendCheckChange--;
  385. }
  386. this.checkChange();
  387. },
  388. <span id='Ext-form-field-Field-method-isFileUpload'> /**
  389. </span> * Returns whether this Field is a file upload field; if it returns true, forms will use special techniques for
  390. * {@link Ext.form.Basic#submit submitting the form} via AJAX. See {@link Ext.form.Basic#hasUpload} for details. If
  391. * this returns true, the {@link #extractFileInput} method must also be implemented to return the corresponding file
  392. * input element.
  393. * @return {Boolean}
  394. */
  395. isFileUpload: function() {
  396. return false;
  397. },
  398. <span id='Ext-form-field-Field-method-extractFileInput'> /**
  399. </span> * Only relevant if the instance's {@link #isFileUpload} method returns true. Returns a reference to the file input
  400. * DOM element holding the user's selected file. The input will be appended into the submission form and will not be
  401. * returned, so this method should also create a replacement.
  402. * @return {HTMLElement}
  403. */
  404. extractFileInput: function() {
  405. return null;
  406. },
  407. <span id='Ext-form-field-Field-method-markInvalid'> /**
  408. </span> * @method markInvalid
  409. * Associate one or more error messages with this field. Components using this mixin should implement this method to
  410. * update the component's rendering to display the messages.
  411. *
  412. * **Note**: this method does not cause the Field's {@link #validate} or {@link #isValid} methods to return `false`
  413. * if the value does _pass_ validation. So simply marking a Field as invalid will not prevent submission of forms
  414. * submitted with the {@link Ext.form.action.Submit#clientValidation} option set.
  415. *
  416. * @param {String/String[]} errors The error message(s) for the field.
  417. */
  418. markInvalid: Ext.emptyFn,
  419. <span id='Ext-form-field-Field-method-clearInvalid'> /**
  420. </span> * @method clearInvalid
  421. * Clear any invalid styles/messages for this field. Components using this mixin should implement this method to
  422. * update the components rendering to clear any existing messages.
  423. *
  424. * **Note**: this method does not cause the Field's {@link #validate} or {@link #isValid} methods to return `true`
  425. * if the value does not _pass_ validation. So simply clearing a field's errors will not necessarily allow
  426. * submission of forms submitted with the {@link Ext.form.action.Submit#clientValidation} option set.
  427. */
  428. clearInvalid: Ext.emptyFn
  429. });
  430. </pre>
  431. </body>
  432. </html>