useFieldViolation.ts 865 B

1234567891011121314151617181920212223242526272829
  1. import {computed, ComputedRef} from "@vue/reactivity";
  2. import {useFormStore} from "~/stores/form";
  3. import * as _ from 'lodash-es'
  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 _.get(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. * @param value
  17. */
  18. function updateViolationState(field: string, value: any) {
  19. //@ts-ignore
  20. useFormStore().setViolations(_.omit(useFormStore().violations, field))
  21. }
  22. return {
  23. fieldViolations,
  24. updateViolationState: (fieldValue: any) => updateViolationState(fieldValue, field)
  25. }
  26. }