Guest User

Untitled

a guest
Dec 10th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. export const formMixin = {
  2. data () {
  3. return {
  4. isStoring: false,
  5. serverValidationErrors: {},
  6. responseMessage: null,
  7. responseStatus: null,
  8. formFields: {},
  9. }
  10. },
  11.  
  12. created() {
  13.  
  14.  
  15. },
  16. methods: {
  17.  
  18. resetFlags(){
  19.  
  20. for (let key of Object.keys(this.fields)){
  21.  
  22. this.$validator.flag(key, {
  23. "untouched": true,
  24. "touched": false,
  25. "dirty": false,
  26. "valid": true,
  27. "invalid": false,
  28. "validated": false
  29. });
  30. }
  31. },
  32. beforeStoreHandler(){
  33. this.serverValidationErrors = {};
  34. this.serverError = null;
  35. this.isStoring = true;
  36. this.responseMessage = null;
  37. this.responseStatus = null;
  38. },
  39. storeErrorHandler(error){
  40. this.isStoring = false;
  41.  
  42. this.responseStatus = 'error';
  43.  
  44. if (error.response && error.response.data && error.response.data.errors) {
  45. this.serverValidationErrors = error.response.data.errors;
  46. }
  47.  
  48. if ((error.response && error.response.data && error.response.data.message) || error.message) {
  49.  
  50. this.responseMessage = error.response.data.message || error.message;
  51. }
  52. },
  53. storeSuccessHandler(response){
  54. this.isStoring = false;
  55.  
  56. this.responseStatus = 'success';
  57.  
  58. // this.formFields = {};
  59.  
  60. if (response && response.message) {
  61. this.responseMessage = response.message;
  62. }
  63.  
  64. // if (response && response.redirect) {
  65. // window.location.replace(response.redirect);
  66. // }
  67. },
  68. getErrors(name, scope = null){
  69.  
  70. let errors = [];
  71.  
  72. if (this.errors.has(`${scope ? `${scope}.` : ''}${name}`)){
  73.  
  74. errors.push(this.errors.first(`${scope ? `${scope}.` : ''}${name}`));
  75. }
  76.  
  77. if (this.serverValidationErrors[name]){
  78.  
  79. for (let index in this.serverValidationErrors[name]){
  80. errors.push(this.serverValidationErrors[name][index]);
  81. }
  82. }
  83.  
  84. return errors;
  85. },
  86. resetForm() {
  87. this.$validator.reset();
  88. this.serverValidationErrors = {};
  89. this.serverError = null;
  90. this.responseMessage = null;
  91. this.responseStatus = null;
  92. this.formFields = {};
  93. }
  94. },
  95. };
Add Comment
Please, Sign In to add comment