useFieldViolation.ts 799 B

123456789101112131415161718192021222324252627282930
  1. import { computed } from 'vue'
  2. import type { ComputedRef } from 'vue'
  3. import * as _ from 'lodash-es'
  4. import { useFormStore } from '~/stores/form'
  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. */
  18. function updateViolationState(field: string) {
  19. useFormStore().setViolations(
  20. _.omit(useFormStore().violations, field) as string[],
  21. )
  22. }
  23. return {
  24. fieldViolations,
  25. updateViolationState,
  26. }
  27. }