useFieldViolation.ts 902 B

123456789101112131415161718192021222324252627282930
  1. import {computed} from "@vue/reactivity";
  2. import type {ComputedRef} from "@vue/reactivity";
  3. import {useFormStore} from "~/stores/form";
  4. import * as _ from 'lodash-es'
  5. /**
  6. * Composable pour gérer l'apparition de message d'erreurs de validation d'un champ de formulaire
  7. *
  8. * @param field
  9. */
  10. export function useFieldViolation(field: string) {
  11. const fieldViolations: ComputedRef<string> = computed(()=> {
  12. return _.get(useFormStore().violations, field, '')
  13. })
  14. /**
  15. * Lorsque la valeur d'un champ change, on supprime le fait qu'il puisse être "faux" dans le store
  16. * @param field
  17. * @param value
  18. */
  19. function updateViolationState(field: string, value: any) {
  20. //@ts-ignore
  21. useFormStore().setViolations(_.omit(useFormStore().violations, field))
  22. }
  23. return {
  24. fieldViolations,
  25. updateViolationState: (fieldValue: any) => updateViolationState(fieldValue, field)
  26. }
  27. }