useFieldViolation.ts 836 B

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