- Newest
- Most votes
- Most comments
You're on the right track with your two possible solutions. Let's explore each one in more detail:
-
Passing custom value into formData
- You can leverage the
useAuthenticatorhook provided by Amplify Authenticator to access theformDataandvalidationErrorsobjects. - Within your custom component, you can update the
formDataobject with your custom field value and also handle the validation logic. - Here's an example of how you can achieve this:
import { useAuthenticator } from '@aws-amplify/ui-react'; const CustomToggleButtons = () => { const { formData, setFormData, validationErrors, setValidationErrors } = useAuthenticator(); const handleToggleChange = (value) => { setFormData((prevFormData) => ({ ...prevFormData, customField: value, })); // Validate the custom field if (!value) { setValidationErrors((prevErrors) => ({ ...prevErrors, customField: 'This field is required', })); } else { setValidationErrors((prevErrors) => ({ ...prevErrors, customField: undefined, })); } }; return ( <div> <ToggleButtons value={formData.customField} onChange={handleToggleChange} /> </div> ); }; - You can leverage the
-
Inheriting from the
Fieldcomponent:- Amplify Authenticator's
Fieldcomponent is a wrapper around theFormFieldcomponent from the@aws-amplify/ui-reactlibrary. - You can create your own custom field component that extends the
FormFieldcomponent and integrates your custom input (e.g., ToggleButtons). - Here's an example of how you can do this:
import { FormField } from '@aws-amplify/ui-react'; const CustomField extends FormField { render() { return ( <FormField {...this.props}> <ToggleButtons value={this.props.value} onChange={this.props.onChange} /> </FormField> ); } } // Then, use your custom field in the Authenticator <Authenticator> <CustomField name="customField" label="Custom Field" errorMessage={validationErrors.customField as string} hasError={!!validationErrors.customField} /> </Authenticator> - Amplify Authenticator's
Both solutions have their advantages. The first approach (updating formData directly) gives you more control over the validation and submission process, but you'll need to integrate it with the Authenticator's state management. The second approach (inheriting from Field) allows you to leverage the existing Authenticator functionality more seamlessly, but you'll need to ensure your custom field component is compatible with the Authenticator's requirements.
the choice will depend on your specific use case and preference. I'd recommend trying both approaches and seeing which one works best for your application.
Relevant content
- asked 9 months ago
- asked 6 months ago
- AWS OFFICIALUpdated 6 months ago

Output fields
formData, setFormData, setValidationErrorsdo not exist on useAuthenticator.