useFieldViolation.ts 801 B

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