useFieldViolation.ts 753 B

123456789101112131415161718192021222324252627
  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, '') as string
  13. })
  14. /**
  15. * Lorsque la valeur d'un champ change, on supprime le fait qu'il puisse être "faux" dans le store
  16. */
  17. function updateViolationState() {
  18. useFormStore().setViolations(_.omit(useFormStore().violations, field))
  19. }
  20. return {
  21. fieldViolations,
  22. updateViolationState,
  23. }
  24. }