Unform
Get/set field error
Get/set field error
Unform provides an easy way to set or access input errors by using the form ref.
1export default function MyForm() {2 const formRef = useRef(null);34 function functionThatGetsErrors() {5 // Get all errors6 const allErrors = formRef.current.getErrors();78 // Get single field error9 const name = formRef.current.getFieldError('name');10 const street = formRef.current.getFieldError('address.street');1112 console.log(allErrors, name, street);13 }1415 function functionThatSetsErrors() {16 // Set all errors17 formRef.current.setErrors({18 name: 'Name is required',19 address: {20 street: 'Street not found',21 },22 });2324 // Set single field error25 formRef.current.setFieldError('name', 'Name is required');26 formRef.current.setFieldError('address.street', 'Street not found');27 }2829 return (30 <Form ref={formRef}>31 <Input name="name" />3233 <Scope path="address">34 <Input name="street" />35 <Input name="number" />36 </Scope>37 </Form>38 );39}