useFieldViolation.ts 768 B

1234567891011121314151617181920212223242526272829
  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. */
  17. function updateViolationState() {
  18. useFormStore().setViolations(
  19. _.omit(useFormStore().violations, field) as string[],
  20. )
  21. }
  22. return {
  23. fieldViolations,
  24. updateViolationState,
  25. }
  26. }