Skip to content

Commit

Permalink
feat: expose getError, getValue, getErrors on form groups
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Feb 7, 2025
1 parent 141e5fb commit e873ea0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-eels-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@formwerk/core': patch
---

feat: expose getError, getValue, getErrors on form groups
28 changes: 20 additions & 8 deletions packages/core/src/useFormGroup/useFormGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function useFormGroup<TInput extends FormObject = FormObject, TOutput ext
toValue(props.disableHtmlValidation) ?? form?.isHtmlValidationDisabled() ?? getConfig().disableHtmlValidation;
const { validate, onValidationDispatch, defineValidationRequest, onValidationDone, dispatchValidateDone } =
useValidationProvider({
getValues,
getValues: () => getValue(),
getPath,
schema: props.schema,
type: 'GROUP',
Expand Down Expand Up @@ -132,24 +132,24 @@ export function useFormGroup<TInput extends FormObject = FormObject, TOutput ext
);
});

function getValues(): TInput {
return form?.getValue(getPath()) ?? {};
}

function getErrors() {
return form?.getErrors(getPath()) ?? [];
}

function getValue(path?: string) {
return form?.getValue(prefixPath(path) ?? '');
}

const isValid = computed(() => getErrors().length === 0);
const isTouched = computed(() => form?.isTouched(getPath()) ?? false);
const isDirty = computed(() => {
const path = getPath();

return !isEqual(getValues(), form?.getFieldOriginalValue(path) ?? {});
return !isEqual(getValue(), form?.getFieldOriginalValue(path) ?? {});
});

function getError(name: string) {
return form?.getErrors(prefixPath(name) ?? '')?.[0];
function getError(path: string) {
return form?.getErrors(prefixPath(path) ?? '')?.[0];
}

function displayError(name: string) {
Expand Down Expand Up @@ -228,5 +228,17 @@ export function useFormGroup<TInput extends FormObject = FormObject, TOutput ext
* Validates the form group.
*/
validate,
/**
* Gets the errors for the form group.
*/
getErrors,
/**
* Gets the group's value, passing in a path will return the value of that field.
*/
getValue,
/**
* Gets the error for a given field.
*/
getError,
};
}
2 changes: 1 addition & 1 deletion packages/playground/src/components/FormGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ import { FormGroupProps, useFormGroup } from '@formwerk/core';
const props = defineProps<FormGroupProps>();
const { labelProps, groupProps, getErrors, getError, displayError, getValues, isValid, isDirty, isTouched } =
const { labelProps, groupProps, getErrors, getError, displayError, getValue, isValid, isDirty, isTouched } =
useFormGroup(props);
</script>

0 comments on commit e873ea0

Please sign in to comment.