Advertisement
Guest User

Untitled

a guest
May 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. class Errors {
  2. /**
  3. * Create a new Errors instance.
  4. */
  5. constructor() {
  6. this.errors = {};
  7. }
  8.  
  9. /**
  10. * Determine if an errors exists for the given field.
  11. *
  12. * @param {string} field
  13. */
  14. has(field) {
  15. return this.errors.hasOwnProperty(field);
  16. }
  17.  
  18. /**
  19. * Determine if we have any errors.
  20. */
  21. any() {
  22. return Object.keys(this.errors).length > 0;
  23. }
  24.  
  25. /**
  26. * Retrieve the error message for a field.
  27. *
  28. * @param {string} field
  29. */
  30. get(field) {
  31. if (this.errors[field]) {
  32. return this.errors[field][0];
  33. }
  34. }
  35.  
  36. /**
  37. * Record the new errors.
  38. *
  39. * @param {object} errors
  40. */
  41. record(errors) {
  42. this.errors = errors;
  43. }
  44.  
  45. /**
  46. * Clear one or all error fields.
  47. *
  48. * @param {string|null} field
  49. */
  50. clear(field) {
  51. if (field) {
  52. delete this.errors[field];
  53. return;
  54. }
  55. this.errors = {};
  56. }
  57. }
  58.  
  59. class Form {
  60. /**
  61. * Create a new Form instance.
  62. *
  63. * @param {object} data
  64. */
  65. constructor(data) {
  66. this.originalData = data;
  67. for (let field in data) {
  68. this[field] = data[field];
  69. }
  70. this.errors = new Errors();
  71. }
  72.  
  73. /**
  74. * Fetch all relevant data for the form.
  75. */
  76. data() {
  77. let data = {};
  78. for (let property in this.originalData) {
  79. data[property] = this[property];
  80. }
  81. return data;
  82. }
  83.  
  84. /**
  85. * Reset the form fields.
  86. */
  87. reset() {
  88. for (let field in this.originalData) {
  89. this[field] = '';
  90. }
  91. this.errors.clear();
  92. }
  93.  
  94. /**
  95. * Send a POST request to the given URL.
  96. * .
  97. * @param {string} url
  98. */
  99. post(url) {
  100. return this.submit('post', url);
  101. }
  102.  
  103. /**
  104. * Send a PUT request to the given URL.
  105. * .
  106. * @param {string} url
  107. */
  108. put(url) {
  109. return this.submit('put', url);
  110. }
  111.  
  112. /**
  113. * Send a PATCH request to the given URL.
  114. * .
  115. * @param {string} url
  116. */
  117. patch(url) {
  118. return this.submit('patch', url);
  119. }
  120.  
  121. /**
  122. * Send a DELETE request to the given URL.
  123. * .
  124. * @param {string} url
  125. */
  126. delete(url) {
  127. return this.submit('delete', url);
  128. }
  129.  
  130. /**
  131. * Submit the form.
  132. *
  133. * @param {string} requestType
  134. * @param {string} url
  135. */
  136. submit(requestType, url) {
  137. return new Promise((resolve, reject) => {
  138. window.axios[requestType](url, this.data())
  139. .then(response => {
  140. this.onSuccess(response.data);
  141. resolve(response.data);
  142. })
  143. .catch(error => {
  144. this.onFail(error.response.data);
  145. reject(error.response.data);
  146. });
  147. });
  148. }
  149.  
  150. /**
  151. * Handle a successful form submission.
  152. *
  153. * @param {object} data
  154. */
  155. onSuccess(data) {
  156. // in Laravel controller do
  157. // return ['message' => 'success'];
  158. // alert(data.message);
  159. this.reset();
  160. }
  161.  
  162. /**
  163. * Handle a failed form submission.
  164. *
  165. * @param {object} errors
  166. */
  167. onFail(errors) {
  168. this.errors.record(errors);
  169. }
  170. }
  171.  
  172. export default Form;
  173.  
  174. // HOW TO USE IN VUE COMPONENT:
  175. // data : {
  176. // form : new Form({
  177. // name : '',
  178. // description : '',
  179. // }),
  180. // },
  181.  
  182. // methods : {
  183. // onSubmit() {
  184. // this.form.post('/projects')
  185. // .then(response => alert('Wahoo!'));
  186. // }
  187. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement