Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. import { formValidationRules } from "../form-validation.rules";
  2. import { BBTextInput } from "../controls/text-input/text-input";
  3. import { BBNumberInput } from "../controls/number-input/number-input";
  4. import { BBFileInput } from "../controls/file-input/file-input";
  5. import { BBTextArea } from "../controls/text-area/text-area";
  6. import { BBDropdown } from "../controls/dropdown/dropdown";
  7. import { BBDropdownMultiselect } from "../controls/dropdownMultiselect/dropdown";
  8. import { BBDateInput } from "../controls/date-input/date-input";
  9. import { BBCheckbox } from "../controls/checkbox/checkbox";
  10. import { BBRadioButton } from "../controls/radio-button/radio-button";
  11.  
  12. export class BBFormUtils {
  13. static createErrorMessage = (message, context) => {
  14. const matches = message.match(/\{(.*?)\}/g);
  15. let parsedMessage = message;
  16. if (matches) {
  17. matches.forEach((match, index) => {
  18. const matchValue = match.slice(1, -1).trim();
  19.  
  20. parsedMessage = parsedMessage.replace(match, context[matchValue]);
  21. });
  22. }
  23. return parsedMessage;
  24. };
  25.  
  26. static createValidator(rule, parameters, errorMessage) {
  27. const validator = formValidationRules[rule];
  28. if (!validator) {
  29. return null;
  30. }
  31.  
  32. const validatorParameters = Array.isArray(parameters)
  33. ? parameters
  34. : [parameters];
  35.  
  36. return validator(...validatorParameters, errorMessage);
  37. }
  38.  
  39. static getFormComponent(controlType) {
  40. switch (controlType) {
  41. case "text":
  42. return BBTextInput;
  43.  
  44. case "number":
  45. return BBNumberInput;
  46.  
  47. case "textarea":
  48. return BBTextArea;
  49.  
  50. case "radiobutton":
  51. return BBRadioButton;
  52.  
  53. case "date":
  54. return BBDateInput;
  55.  
  56. case "checkbox":
  57. return BBCheckbox;
  58.  
  59. case "dropdown_multiselect":
  60. return BBDropdownMultiselect;
  61.  
  62. case "file":
  63. return BBFileInput;
  64.  
  65. case "dropdown":
  66. return BBDropdown;
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement